MATLAB Answers

How I can find index of element in array?

9,916 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:
I realized this function, using for loop and if statement, but did Matlab doesn't have build-in similar function?

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!