MATLAB Answers


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

Asked by GuillyT
on 28 Oct 2019
Latest activity Edited by Rik
on 28 Oct 2019
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.


Are app1, dApp, and torque1 all arrays of the same size? If so, the following would provide the indexes of meeting the conditions.
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.

Sign in to comment.

1 Answer

Answer by Rik
on 28 Oct 2019
Edited by Rik
on 28 Oct 2019
 Accepted Answer

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.


Sign in to comment.