MATLAB Answers

How I can find index of element in array?

조회 수: 8,608(최근 30일)
Mykhailo Yaroshenko
Mykhailo Yaroshenko 2017년 11월 8일
댓글: Ehsan Partovi 2021년 10월 2일 8:03
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?

채택된 답변

James Tursa
James Tursa 2017년 11월 8일
편집: James Tursa 2017년 11월 8일
If you know the number exactly, then you can use:
result = find(X==5);
  댓글 수: 6
Ehsan Partovi
Ehsan Partovi 2021년 10월 2일 8:03
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.

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by