How to extract indices with a condition?

For example I have Y = X(X==42), but I need the indices also!

2 commentaires

Stephen23
Stephen23 le 26 Mar 2018
X==42 gives the logical indices, which will be faster than using find.
rbih rbih
rbih rbih le 12 Juin 2020
hi Mr M. i just wondering about the value that you're searching index for (42), isn't that related to the quiz of rigid body kinematics course (Concept Check 9 - Euler Angle Differential Kinematic Equations) on coursera!
just for fun, have a good day.

Connectez-vous pour commenter.

 Réponse acceptée

Birdman
Birdman le 26 Mar 2018
Modifié(e) : Birdman le 26 Mar 2018
To find the index where X is equal to 42, type
idx=find(X==42)
Y=X(idx)

Plus de réponses (1)

Walter Roberson
Walter Roberson le 26 Mar 2018

1 vote

find(X==42) and store in a variable. Then X(that_variable)

10 commentaires

Mr M.
Mr M. le 26 Mar 2018
Modifié(e) : Mr M. le 26 Mar 2018
Thanks! and what if I have zero-indices ind0 = find(X==0), but I need non-zero indices also. Are there any faster way using ind0, or I have to use another find: find(X~=0) ?
For nonzero indices, simply type
find(X~=0)
Mr M.
Mr M. le 26 Mar 2018
yes, I know, as you can see above, but my question was: is it possible to do faster? by using ind0
Mr M.
Mr M. le 26 Mar 2018
with logical indexing its trivial, but ind0 is a vector of integer indices
Birdman
Birdman le 26 Mar 2018
It is possible to do faster, yes, by using logical indexing.
X(setdiff(1:numels(X), ind0))
Ron
Ron le 23 Fév 2024
can you please this statement? thankyou.
Rik
Rik le 23 Fév 2024
@Ron Your comment seems to be missing some words. If you're asking for an explanation of Walters last comment: did you read the documentation for the functions he used? (There is also probably a typo: numels should be numel)
Ron
Ron le 29 Fév 2024
@Rik, thanks for replying. Yes my comment was somewhat arbitrary here. Yes I was talking about "X(setdiff(1:numels(X), ind0))", this statement. I couldnt understand what it means. Yes you are right about the typo, thanks for correcting me.
setdiff(1:numels(X), ind0)
returns all the indices of X that do not appear in ind0
It is questionable whether this will be any faster than
X(X ~= 0)
but the original poster wanted a different way.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Performance and Memory dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by