Requested array exceeds the maximum possible variable size.

28 views (last 30 days)
I am trying to get the mean value of all pixels in 5D array (arr):
let (f) a 4D array that I want to search for a value in it:
size of arr= (800,800,3,9,9)
size of f = (800,800,9,9)
[x, y, z, w] = ind2sub(size(f),find(f == value));
result = mean(arr(x, y ,3, z ,w),'all');
I receive this error :
Requested array exceeds the maximum possible variable size. when using mean!
any help is appreciated,
thanks

Accepted Answer

Steven Lord
Steven Lord on 24 Jan 2021
Let's say you want to extract the two elements equal to 1 in this matrix.
A = [1 2 3; 4 5 6; 7 8 1]
A = 3×3
1 2 3 4 5 6 7 8 1
You can do this with find and ind2sub.
locations = find(A == 1)
locations = 2×1
1 9
[row, col] = ind2sub(size(A), locations)
row = 2×1
1 3
col = 2×1
1 3
Now let's extract the 1's from A.
B = A(row, col)
B = 2×2
1 3 7 1
Why are the 3 and 7 in there? Because B contains all the elements at the intersections of rows in the variable row and columns in the variable col. So even though you may have expected B to have only two elements because row and col each had two elements, it actually has two times two or four elements.
Now let's look at a slightly different matrix:
C = [1 1 3; 4 5 6; 7 8 1];
locationsC = find(C == 1);
[rowC, colC] = ind2sub(size(C), locationsC)
rowC = 3×1
1 1 3
colC = 3×1
1 2 3
D = C(rowC, colC)
D = 3×3
1 1 3 1 1 3 7 8 1
These are the elements at the intersections of rows 1, 1, and 3 and columns 1, 2, and 3. Given that knowledge, even though arr is of size (800,800,3,9,9) the array arr(x, y ,3, z ,w) may be much larger.
If you just want the mean of elements in an array corresponding to locations of a specific value in another array, don't use subscripted indexing. Use logical indexing.
justThreeElements = A(C == 1)
justThreeElements = 3×1
1 2 1
mean(justThreeElements)
ans = 1.3333
The fact that you have that other dimension in arr is a little tricky, but you could extract just the third page to a separate variable.
  3 Comments
Walter Roberson
Walter Roberson on 25 Jan 2021
Yes. It has to do with how indexing works, and the indexing is done before the outer function such as sum() or mean() is called.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!