Requested array exceeds the maximum possible variable size.

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

 채택된 답변

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

MatlabUser
MatlabUser 2021년 1월 25일
편집: MatlabUser 2021년 1월 25일
Thank you so much for your explanation, it is really helpful !
so it means the output value is not the correct one since it will contain the intersections as well,
thanks !
Does the same story apply to sum() as well?
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

질문:

2021년 1월 24일

댓글:

2021년 1월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by