Largest lindexing supported by MATLAB?

1 view (last 30 days)
What is the largest (linear) indexing supported by MATLAB? I would be good to know if there is an official specification for such thing.
  4 Comments
Jiri Hajek
Jiri Hajek on 9 Dec 2022
After some search and testing, it seems that information provided by the function intmax gives the answer for integers.
Stephen23
Stephen23 on 9 Dec 2022
Edited: Stephen23 on 9 Dec 2022
"it seems that information provided by the function intmax gives the answer for integers. "
How? I don't see anything on that page related to maximum index size (only the unrelated max value). Using the same test that Rik proposed gives exactly the same error message as the default DOUBLE():
A = uint8(0);
A(flintmax) = 1;
One or more indices are invalid. Indices must be less than 2^48.

Sign in to comment.

Accepted Answer

Rik
Rik on 9 Dec 2022
Edited: Rik on 9 Dec 2022
For a double, you can just look at the error message:
try
A=[];
A(flintmax)=1;
catch ME,disp(ME.message),end
One or more indices are invalid. Indices must be less than 2^48.
And if I test for an integer type, it seems that same limit applies, at least on the 64 bit Linux version that the forum uses.
try
A=uint8([]);
A(intmax+1)=1;
catch ME,disp(ME.message),end
try
A=uint8([]);
A(flintmax)=1;
catch ME,disp(ME.message),end
One or more indices are invalid. Indices must be less than 2^48.
After your comment I wanted to see what would happen when trying to create an array beyond the allowed index range, which gave me this error message:
try
S=sparse(2^49,2)
catch ME,disp(ME.message),end
Sparse matrix sizes must be nonnegative integer scalars less than MAXSIZE as defined by COMPUTER. Use HELP COMPUTER for more details.
So apparently we need the computer function:
[str,maxsize] = computer; log2(maxsize) % this is a rounding error, maxsize is 2^48-1
ans = 48.0000
On 32 bit Matlab releases this seems to be 2^31-1.
  2 Comments
Bruno Luong
Bruno Luong on 9 Dec 2022
Edited: Bruno Luong on 9 Dec 2022
Also for sparse but the message is not clear
S=sparse(2^25,2^25)
S =
All zero sparse: 33554432×33554432
S(end,end)
ans =
All zero sparse: 1×1
S(end) % "end" is equivalent to 2^50
Requested array exceeds the maximum possible variable size.
Bruno Luong
Bruno Luong on 9 Dec 2022
Edited: Bruno Luong on 9 Dec 2022
Thanks Rik
[~,maxsize] = computer
is the way to go.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!