Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find subscripts of matching element(s) in matlab/octave

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)

like image 490
Joel Avatar asked Nov 26 '11 23:11

Joel


People also ask

How to get index of element MATLAB?

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.

What is find() 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.


2 Answers

[ix,iy]=find(X==0.26832)
ix = 
    1
iy = 
    3
like image 64
arne.b Avatar answered Oct 15 '22 15:10

arne.b


[i j]=ind2sub(size(X),find(X==0.26832))
like image 43
Oli Avatar answered Oct 15 '22 17:10

Oli