2017-04-26 395 views
2

任何技巧,以避免内存不足错误在MATLAB中? 我假设它出现的原因是因为matlab在使用horzcat时非常低效,实际上需要暂时复制矩阵。Matlab Horzcat - 内存不足

我有一个矩阵A大小为108977555 x 25。我想合并这三个载体d,my各自的大小108977555 x 1

我的机器有32GB ram,上面的matrice +向量占用18GB。

现在我想运行下面的命令:

A = [A(:,1:3), d, m, y, A(:,5:end)]; 

但是,这产生了错误:

Error using horzcat 
Out of memory. Type HELP MEMORY for your options. 

任何把戏做到这一点的合并?

回答

4

Working with Large Data Sets. If you are working with large data sets, you need to be careful when increasing the size of an array to avoid getting errors caused by insufficient memory. If you expand the array beyond the available contiguous memory of its original location, MATLAB must make a copy of the array and set this copy to the new value. During this operation, there are two copies of the original array in memory.

  1. 重启MATLAB,我经常发现它并没有完全清理其内存也得到支离破碎,从而降低最大数组大小。

  2. 更改您的数据类型(如果可以的话)。例如。如果你只用数字0 - 255处理,使用uint8,内存大小由8倍减少相比double小号

  3. 开始的数组与A已经足够大(即108977555x27而不是108977555x25和插入到位:

    A(:, 4) = d; 
    clear d 
    A(:, 5) = m; 
    clear m 
    A(:, 6) = y; 
    
  4. 合并在一个数据类型的数据,以减少总存储器需求,例如日期轻松放入一个uint32

  5. 。 210
  6. 将数据分开,考虑为什么要将数据放在一个矩阵中,以及是否真的有必要。

  7. 使用C-code自己做数据分配(只有当你真的绝望)

延伸阅读:https://nl.mathworks.com/help/matlab/matlab_prog/memory-allocation.html

1

你可以先试试高效的内存管理策略上提到官方网站The MathWorks公司:https://in.mathworks.com/help/matlab/matlab_prog/strategies-for-efficient-use-of-memory.html

  • 使用单(4个字节)或其他一些较小的数据类型,而不是双倍(8字节),如果你的代码可以使用。
  • 如果可能的话,使用块处理(如行或列),即存储块作为单独的mat文件,并只加载和访问需要的矩阵部分。
  • 使用matfile命令加载大variables in parts。也许是这样的:

    save('A.mat','A','-v7.3') 
    oldMat = matfile('A.mat'); 
    clear A 
    newMat = matfile('Anew.mat','Writeable',true) %Empty matfile 
    for i=1:27 
    if (i<4), newMat.A(:,i) = oldMat.A(:,i); end 
    if (i==4), newMat.A(:,i) = d; end 
    if (i==5), newMat.A(:,i) = m; end 
    if (i==6), newMat.A(:,i) = y; end 
    if (i>6), newMat.A(:,i) = oldMat.A(:,i-2); end 
    end 
    
1

即使你可以使用冈瑟的建议吧作,它只是占用内存。现在需要超过一半的可用内存。那么,你打算做什么呢?即使简单B = A+1不适合。你唯一能做的就是像sum这样的东西,或者对阵列的一部分进行操作。

因此,您应该考虑去tall arrays和其他相关的大数据概念,这些概念正好适用于如此庞大的数据集。

https://www.mathworks.com/help/matlab/tall-arrays.html