Given a value in a matrix how can you get the subscript(s) at which the value occurs in the matrix?
So in this example
octave:27> X=rand(3)
X =
0.46749 0.41187 0.26832
0.91106 0.63567 0.97302
0.71809 0.55269 0.84742
Given the value 0.26832 I would like to extract the subscript (1,3)
In MATLAB the array indexing starts from 1. To find the index of the element in the array, you can use the find() function. Using the find() function you can find the indices and the element from the array. The find() function returns a vector containing the data.
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.
[ix,iy]=find(X==0.26832)
ix =
1
iy =
3
[i j]=ind2sub(size(X),find(X==0.26832))
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