Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding the index of the smallest element in a 3-dimensional matrix (or n-dimensional)

I have a matrix D(i,j,k) and I want to find i,j,k so as to minimize x:

x = D(i,j,k)

For example:

D = rand(10,10,10);
min(min(min(D))) = 0.5123; %The smallest element in D

What I want to know is the index of D that gives 0.5123

How can I do this? Thanks, Elliot

like image 280
Elliot Gorokhovsky Avatar asked Jan 03 '14 19:01

Elliot Gorokhovsky


People also ask

How do you find the index of the smallest number in an array in Matlab?

M = min( A ) returns the minimum elements of an array. If A is a vector, then min(A) returns the minimum of A . If A is a matrix, then min(A) is a row vector containing the minimum value of each column of A .

What is the minimum index in an array?

min index(es) is the index for the first min value. If array is multidimensional, min index(es) is an array whose elements are the indexes for the first minimum value in array.

How do you find the index of a maximum value in Matlab?

You can use max() to get the max value. The max function can also return the index of the maximum value in the vector. To get this, assign the result of the call to max to a two element vector instead of just a single variable.

How do you find the indices of a number in Matlab?

k = find( X , n ) returns the first n indices corresponding to the nonzero elements in X . k = find( X , n , direction ) , where direction is 'last' , finds the last n indices corresponding to nonzero elements in X .


1 Answers

Try min with the colon operator, then ind2sub:

[xmin,ind] = min(D(:));
[ii,jj,kk] = ind2sub(size(D),ind)
like image 80
chappjc Avatar answered Sep 25 '22 15:09

chappjc