Issue with finding the indices of the minimum element in a 3-Dimensional Array

For a 2D array A, the command
[row,column]=find(A==min(min(A)))
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
find(variance_matrix==min(min(min(variance_matrix))))
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.

 Accepted Answer

A = rand(10,10,11);
B = A(:);
index = find(A==min(B))
index = 990
B(index)
ans = 2.4262e-04
[i j k] = ind2sub([10 10 11],index)
i = 10
j = 9
k = 10
A(i,j,k)
ans = 2.4262e-04

4 Comments

Thank you, this works! But I'm curious what went wrong with the approach I've tried.
@PASUNURU SAI VINEETH The find command is not designed for more than 2D array.
So when you try on A_3D of size (m x n x p)
[i,j,page] = find(A_3d);
Matlab reshape A_3D to 2D array:
A_2D = A_3D(:,:)
or more precisely
A_2D = reshape(A_3D, [m, n*p])
The user command
[i,j,page] = find(A_3d);
is acutally performed like following
[i,j,value] = find( reshape(A_3D, [m, n*p]) );
page = value;
Therefore
  • j can take values from 1 to n*p (and not n)
  • and what you think page returned is actually the value of A_3D that are not equal to 0, in your case class logical (all TRUE) since your array A_3D is of class logical.
To check it you could fix your command (beside Torsen's solution) by
[i,j] = find(A_3d);
s = size(A_3d);
s = s(2:end); % n x p, p must be > 1
[j,page] = ind2sub(s, j);

Sign in to comment.

More Answers (1)

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])
A =
A(:,:,1) = 5 13 9 3 23 6 A(:,:,2) = 22 15 8 24 19 16 A(:,:,3) = 11 21 1 14 2 10 A(:,:,4) = 18 12 20 7 4 17
[value, ind] = min(A, [], 'all', 'linear')
value = 1
ind = 14
A(ind)
ans = 1
If you need the subscripts instead of the linear index, use ind2sub.
[row, column, page] = ind2sub(size(A), ind)
row = 2
column = 1
page = 3

Categories

Find more on Sparse Matrices in Help Center and File Exchange

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!