MATLAB Answers

How I can find index of element in array?

10.115 views (last 30 days)
I know, that number, for example, 5, is an element in array X, but I don't know it's index. In Python, I can use:
X.index(5)
I realized this function, using for loop and if statement, but did Matlab doesn't have build-in similar function?

  0 Comments

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 8 Nov 2017
Edited: James Tursa on 8 Nov 2017
If you know the number exactly, then you can use:
result = find(X==5);

  1 Comment

Stephen Cobeldick
Stephen Cobeldick on 9 Nov 2017
Often logical indexing is more efficient, so you might only need this:
idx = X==5;

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by