2017-04-26 392 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