Effacer les filtres
Effacer les filtres

how to replace "&&" in a loop

1 vue (au cours des 30 derniers jours)
Alan Robertson
Alan Robertson le 6 Mar 2020
Modifié(e) : Adam Danz le 6 Mar 2020
I would like not to use "&&" in this loop:
if a=x && b=y
d=2
end
how can i replace it?

Réponses (1)

Adam Danz
Adam Danz le 6 Mar 2020
Modifié(e) : Adam Danz le 6 Mar 2020
"how to replace "&&" in a loop"
If, for whatever reason, you don't like the && symbols,
if a==x && b==y
d=2;
end
could be rewritten as
if all([a==x,b==y])
or
if all([isequal(a,x), isequal(b,y)])
or perhaps you'd like to use indexing as Stephen pointed out,
idx = a==x & b==y;
d(idx) = 2;
  2 commentaires
Stephen23
Stephen23 le 6 Mar 2020
Modifié(e) : Stephen23 le 6 Mar 2020
If any of a, b, x, or y are non-scalar then your proposal will give a very different response to the original code.
I suspect that the OP actually has non-scalar data, and that indexing would be a much more suitable solution.
Adam Danz
Adam Danz le 6 Mar 2020
The original code would throw an error at if a=x but I'll up date my answer to cover the indexing interpretation.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by