How I can find index of element in array?
5.776 views (last 30 days)
Show older comments
Mykhailo Yaroshenko
on 8 Nov 2017
Commented: Ehsan Partovi
on 2 Oct 2021
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
Accepted Answer
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);
6 Comments
Ehsan Partovi
on 2 Oct 2021
The function find() is useful as far as matrices (2-D tensors) are concerned. I cannot, however, find a useful function for nd-arrays where, for instance, the index could be an array on its own. See example below:
M = reshape(1:24, [2,3,4]);
indices = index_finder(M==20); % indices = vector of indices
It would be very useful if there was a function which worked for tensors of any dimensionality.
More Answers (0)
See Also
Categories
Find more on Matrices and Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!