how do I find the index of all values which meet a condition within an array ?

조회 수: 189 (최근 30일)
GuillyT
GuillyT 2019년 10월 28일
편집: Rik 2019년 10월 28일
So I have the follow statement
cond = (app1>0 & app1<100)& dApp>0 & torque1_req >97;
iTp(:,1) = torque1(cond);
I want to know the index of all the values within torque1 which fufill the statement.
  댓글 수: 2
David Hill
David Hill 2019년 10월 28일
Are app1, dApp, and torque1 all arrays of the same size? If so, the following would provide the indexes of meeting the conditions.
idx=find((app1>0).*(app1<100).*(dApp>0).*(torque1>97));
Rik
Rik 2019년 10월 28일
I haven't done any testing, but it seems unlikely to me that a multiplication would have a better perfomance than the logical operators.
It does indeed seem like find is what GuillyT is looking for.

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

채택된 답변

Rik
Rik 2019년 10월 28일
편집: Rik 2019년 10월 28일
With the find function you can look up the linear indices of all non-zero elements in an array. (you can also look up row and column indices, but for higher dimensions you'll need findND) Because of logical indexing, this step is not needed for the indexing itself, but for other cases it is a very powerfull tool.
This should be what you're looking for.
idx=find(cond);

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by