Issue with finding the indices of the minimum element in a 3-Dimensional Array
2 views (last 30 days)
For a 2D array A, the command
outputs the row and column indices of the minimum respectively. But using a similar technique isn't working for a 3D array. For a 10x10x11 array variance_matrix (file attached), the command
outputs the location "151" which I presume represents the element in 2nd page/sheet, 5th column and 1st row.
But, the command
[row,col,depth] = find(variance_matrix==min(min(min(variance_matrix))));
outputs an absurd answer. "depth" is shown to be a logical operator while "col" (16) exceeds the actual column size (10).
I'm hoping someone would explain what's going wrong and if there's a right way to do it for multidimensional (>=3) arrays.
More Answers (1)
Steven Lord on 3 Sep 2022
Use the 'all' dimension argument and the 'linear' index argument to obtain the linear index of the maximum of the array considering the data in all dimensions.
A = reshape(randperm(24), [3 2 4])
[value, ind] = min(A, , 'all', 'linear')
If you need the subscripts instead of the linear index, use ind2sub.
[row, column, page] = ind2sub(size(A), ind)