Indexing any true in logical vector

12 views (last 30 days)
Is there an easy way of reducing the true values in logical vector so as only one true would remain?
Example:
A = [1 2 3 4 5 6];
v = [1 0 1 0 1 0];
Now I need to pick either 1, 3 or 5 from A, I don't care wich one. Insted of using find:
ind_v = find(v);
Result = A(ind_v(1));
I'd like to do it more elegant, like
Result = A(FirstTrue(v));
Is there a way or should I stick with find? THX

Accepted Answer

Walter Roberson
Walter Roberson on 16 Mar 2020
Edited: Walter Roberson on 16 Mar 2020
Result = A(find(v,1))
I seem to recall that Jan at one point had some interesting timing results on
[temp, idx] = max(v);
if temp > 0
Result = A(idx)
else
Result = [];
end

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!