tags:

views:

31

answers:

2

i have a matrix lets say

x =

 2     2     3
 4     3     2
 6     4     8

now i want to get the location of a number 4. like i want ans like this

ans=(2,1) (3,2)

as these are the locations for 4 in matrix.

+1  A: 

ismember will return an array of 1 or 0 depending on if the cell value there is or isn't the value you're searching for:

octave:9> x
x =

   2   2   3
   4   3   2
   6   4   8

octave:10> ismember(x,4)
ans =

   0
   1
   0
   0
   0
   1
   0
   0
   0

And then you can use find and ind2sub to get the array indicies of the 1s:

octave:11> [i,j] = ind2sub(size(x),find(ismember(x,4)))
i =

   2
   3

j =

   1
   2

So that the indicies are (2,1) and (3,2).

Jonathan Dursi
Actually, I guess you can even skip the ismember bit and just use find: [i,j] = ind2sub(size(x),find(x==4))
Jonathan Dursi
+3  A: 

[i,j] = find(x == 4)

Alex

related questions