Meysam
Meysam

Reputation: 18177

Matlab: How to find indices of a specific value in vector

How can I find the indices of a specific value in a vector? For example in the following vector:

B = [2 3 4 5 2 7 9 2]

I need the index of all occurrences of 2, which is: [1 5 8]

Upvotes: 0

Views: 2388

Answers (1)

Sardar Usama
Sardar Usama

Reputation: 19689

find can be used for this purpose as follows:

find(B==2)

or an alternative:

ind = 1:numel(B);
ind(B==2)

Upvotes: 5

Related Questions