I have an array of labels [1 and 0]. I want to find the indices of all 1s in the array. What Matlab function should I use?

E.g.

a=[1 0 1 0 0 0 1 1 1 1 1 0 0 0]

E.g.

a=[1 0 1 0 0 0 1 1 1 1 1 0 0 0]

+1 vote

Best answer

You can use the function **find()** to find the indices of 1s in the array.

Here is an example:

>> a=[1 0 1 0 0 0 1 1 1 1 1 0 0 0]

a =

1 0 1 0 0 0 1 1 1 1 1 0 0 0>> i=find(a==1)

i =

1 3 7 8 9 10 11

If you want to find the first n indices, you can also mention that as a parameter of the function.

>> i=find(a==1, 2)

i =

1 3