Error:Maximum variable size allowed by the program is exceeded. while using sub2ind

僤鯓⒐⒋嵵緔 提交于 2019-12-24 04:31:31

问题


Please suggest how to sort out this issue:

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.

回答1:


I think the problem is 32/64-bit related. If you have a 32 bit processor, you can address at most

2^32 = 4.294967296e+09

elements. If you have a 64-bit processor, this number increases to

2^64 = 9.223372036854776e+18

Unfortunately, for reasons that are at best vague to me, Matlab does not use this full range. To find out the actual range used by Matlab, issue the following command:

[~,maxSize] = computer

On a 32-bit system, this gives

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

and on a 64-bit system, it gives

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

So apparently, on a 32-bit system, Matlab only uses 31 bits to address elements, which gives you the upper limit.

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 :)

Internally, Matlab always uses linear indices to access elements in an array (it probably just uses a C-style array or so), which implies for your adj matrix that its final element is

finEl = nNodes*nNodes = 2.54016e+09

This, unfortunately, is larger than the maximum addressable with 31 bits. Therefore, on the 32-bit system,

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

while this command poses no problem at all on the 64-bit system.

You'll have to use a workaround on a 32-bit system:

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);

I just had the idea to cast all this into a proper class, so that you can use much more intuitive syntax...but that's a whole lot more than I have time for now :)




回答2:


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

This worked fine...

And, if we have to access the last element of the sparse matrix, we can access by adj(nNodes, nNodes), but adj(nNodes * nNodes) throws error.



来源:https://stackoverflow.com/questions/13280833/errormaximum-variable-size-allowed-by-the-program-is-exceeded-while-using-sub2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!