Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: Finding coordinates of value in multidimensional array

I have a three-dimensional array, and I'd like to be able to find a specific value and get the three coordinates.

For example, if I have:

A = [2 4 6; 8 10 12]

A(:,:,2) = [5 7 9; 11 13 15]

and I want to find where 7 is, I'd like to get the coordinates i = 1 j = 2 k = 2

I've tried variations of find(A == 7), but I haven't got anywhere yet.

Thanks!

like image 318
bull_pucky Avatar asked Sep 08 '11 06:09

bull_pucky


People also ask

Can MATLAB handle multidimensional arrays?

A multidimensional array in MATLAB® is an array with more than two dimensions. In a matrix, the two dimensions are represented by rows and columns. Each element is defined by two subscripts, the row index and the column index.

How do you find the number of non zero elements in a vector in MATLAB?

Description. N = nnz( X ) returns the number of nonzero elements in matrix X .

What does the Find function do in MATLAB?

k = find( X ) returns a vector containing the linear indices of each nonzero element in array X . If X is a vector, then find returns a vector with the same orientation as X . If X is a multidimensional array, then find returns a column vector of the linear indices of the result.


1 Answers

The function you seek is ind2sub:

[i,j,k]=ind2sub(size(A), find(A==7))
i =
     1
j =
     2
k =
     2
like image 164
carlpett Avatar answered Sep 30 '22 23:09

carlpett