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!
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.
Description. N = nnz( X ) returns the number of nonzero elements in matrix X .
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.
The function you seek is ind2sub
:
[i,j,k]=ind2sub(size(A), find(A==7))
i =
1
j =
2
k =
2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With