MATLAB: Search for the coordinates of a value in a multidimensional array

I have a three-dimensional array, and I would like to find a specific value and get 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 , I would like to get the coordinates i = 1 j = 2 k = 2

I tried find(A == 7) options find(A == 7) , but I haven't had anything yet.

Thanks!

+7
source share
2 answers

The requested ind2sub function:

 [i,j,k]=ind2sub(size(A), find(A==7)) i = 1 j = 2 k = 2 
+12
source

You can use find to search for nonzero elements in an array, but this requires a bit of arithmetic. From the documentation:

[row,col] = find(X, ...) returns row and column indices of nonzero entries in the X matrix. This syntax is especially useful when working with sparse matrices. If X is an N-dimensional array with N> 2, col contains linear indexes for the columns. For example, for a 5-by-3 array of X with a non-zero element in X (4,2,3), find returns 4 in the row and 16 in the column. That is (7 columns on page 1) + (7 columns on page 2) + (2 columns on page 3) = 16.

If the matrix M has dimensions axbxc , then the indices (i,j,k) for some value of x are equal to:

 [row,col] = find(A==x); i = row; j = mod(col,b); k = ceil(col/b); 
0
source

All Articles