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

 Réponse acceptée

Walter Roberson
Walter Roberson le 16 Mar 2020
Modifié(e) : Walter Roberson le 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

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Centre d'aide et File Exchange

Produits

Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by