简体   繁体   中英

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]

find can be used for this purpose as follows:

find(B==2)

or an alternative:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM