使用零时出错 内存不足

Error using zeros Out of memory

当我尝试运行宁

Adj = zeros(x*y);

我收到以下错误:

Error using zeros

Out of memory. Type HELP MEMORY for your options.

其中 x*y=37901。我的 PC 存储的占用是

我知道 C 盘空间不大 space 但 34.2 GB 应该足以创建一个 37901*37901 矩阵。

当我 运行 memory 命令时,这就是我得到的:

>> memory
Maximum possible array:        4825 MB (5.059e+09 bytes) *
Memory available for all arrays:        4825 MB (5.059e+09 bytes) *
Memory used by MATLAB:       12369 MB (1.297e+10 bytes)
Physical Memory (RAM):       12218 MB (1.281e+10 bytes)

*  Limited by System Memory (physical + swap file) available.

我该如何解决这个问题? (我使用的是 MATLAB 2017b)

实际上,在编码方面,变量通常存储在内存中(您的计算机RAM)而不是硬盘space。这就是你的错误所抱怨的......你没有足够的内存来存储你想要分配的变量。

Matlab默认使用的数值变量为double,用于表示双精度floating-point值,占用8字节内存。因此,您正在尝试分配:

37901 * 37901 * 8  = 11491886408 bytes
                  ~= 10.7 gigabytes

当您只有 11.9 千兆字节的可用内存时,Matlab 告诉您不能分配大于 4.7 千兆字节的数组。作为解决方法,我建议您查看 Tall Arrays,这是一个针对处理非常大的数据流量身定制的 Matlab 功能:

Tall arrays are used to work with out-of-memory data that is backed by a datastore. Datastores enable you to work with large data sets in small chunks that individually fit in memory, instead of loading the entire data set into memory at once. Tall arrays extend this capability to enable you to work with out-of-memory data using common functions.

What is a Tall Array?

Since the data is not loaded into memory all at once, tall arrays can be arbitrarily large in the first dimension (that is, they can have any number of rows). Instead of writing special code that takes into account the huge size of the data, such as with techniques like MapReduce, tall arrays let you work with large data sets in an intuitive manner that is similar to the way you would work with in-memory MATLAB® arrays. Many core operators and functions work the same with tall arrays as they do with in-memory arrays. MATLAB works with small chunks of the data at a time, handling all of the data chunking and processing in the background, so that common expressions, such as A+B, work with big data sets.

Benefits of Tall Arrays

Unlike in-memory arrays, tall arrays typically remain unevaluated until you request that the calculations be performed using the gather function. This deferred evaluation allows you to work quickly with large data sets. When you eventually request output using gather, MATLAB combines the queued calculations where possible and takes the minimum number of passes through the data. The number of passes through the data greatly affects execution time, so it is recommended that you request output only when necessary.