2012-11-08 89 views
2

請建議如何來解決這個問題:錯誤:超出程序允許的最大變量大小。同時採用sub2ind

nNodes = 50400; 
adj = sparse(nNodes,nNodes); 

adj(sub2ind([nNodes nNodes], ind, ind + 1)) = 1; %ind is a vector of indices 
??? Maximum variable size allowed by the program is exceeded. 
+2

這裏沒有問題,除非'長(ind)'是非常巨大的......你的'ind'有多大? –

+0

它是1x50148。我也將這些索引轉換爲1d並做了以下操作:>> adj(ind1d)= 1;仍然收到相同的錯誤「 該程序允許的最大可變大小爲 超出。」 – Swagatika

回答

2
adj = sparse(ind, ind + 1, ones(size(ind)), nNodes, nNodes, length(ind)); 

這工作得很好......

而且,如果我們要訪問稀疏矩陣的最後一個元素,我們可以通過adj(nNodes,nNodes)訪問,但adj(nNodes * nNodes)會引發錯誤。

3

我認爲問題是32/64位相關。如果你有一個32位的處理器,你最多可以編址

2^32 = 4.294967296e+09 

元素。如果你有一個64位處理器,這個數字上升到

2^64 = 9.223372036854776e+18 

不幸的是,這充其量模糊,我的理由,MATLAB不使用此齊全。要找出利用Matlab實際使用的範圍,發出以下命令:

[~,maxSize] = computer 

在32位系統,這給

>> [~,maxSize] = computer 
maxSize = 
    2.147483647000000e+09 
>> log2(maxSize) 
ans = 
    3.099999999932819e+01 

和64位系統上,它給

>> [~,maxSize] = computer 
maxSize = 
    2.814749767106550e+14 
>> log2(maxSize) 
ans = 
    47.999999999999993 

很顯然,在32位系統上,Matlab只使用31位來尋址元素,這給了你的上限。

If anyone can clarify why Matlab only uses 31 bits on a 32-bit system, and only 48 bits on a 64-bit system, that'd be awesome :)

在內部,Matlab的總​​是使用線性索引來訪問元素的陣列(它可能只是使用C語言風格的陣列或左右),這意味着爲您adj矩陣,它的最後一個元素是

finEl = nNodes*nNodes = 2.54016e+09 

不幸的是,這比31位的最大尋址大。因此,在32位的系統上,

>> adj(end) = 1; 
??? Maximum variable size allowed by the program is exceeded. 

而此命令的64位系統上在所有不會產生問題。

你將不得不使用一種變通方法在32位系統上:

nNodes = 50400; 

% split sparse array up into 4 pieces 
adj{1,1} = sparse(nNodes/2,nNodes/2); adj{1,2} = sparse(nNodes/2,nNodes/2); 
adj{2,1} = sparse(nNodes/2,nNodes/2); adj{2,2} = sparse(nNodes/2,nNodes/2); 

% assign or index values to HUGE sparse arrays 
function ret = indHuge(mat, inds, vals) 

    % get size of cell 
    sz = size(mat); 

    % return current values when not given new values 
    if nargin < 3 
     % I have to leave this up to you... 

    % otherwise, assign new values 
    else 
     % I have to leave this up to you... 

    end 
end  

% now initialize desired elements to 1 
adj = indHuge(adj, sub2ind([nNodes nNodes], ind, ind + 1), 1); 

我只是有想法,投這一切都成真類,這樣就可以使用更直觀的語法。 ..但是這比我現在有更多的時間:)

+0

感謝您解釋系統上發生的事情。是的,我的系統是32位系統,我無法訪問最後一個元素:>> adj(nNodes * nNodes) ???該程序允許的最大可變大小爲 。 – Swagatika

+0

@Swagatika:我將在下一次編輯中加入一個解決方法。刷新5分鐘...:) –

+0

我可以弄清楚如何創建稀疏矩陣,請檢查我的答案,並讓我知道你是否有更優化的答案。仍然訪問最後的元素是一個在這裏的問題... – Swagatika

相關問題