Is there a compact way to check multiple variables against the same conditions?
Ältere Kommentare anzeigen
I'm thinking about how to make some code I'm working on easier to read. For example, if I have something like the following:
if x != 0 && y != 0 && z != 0
% stuff
end
Is there a way for me to turn that into something more compact, like:
if (x&y&z) != 0
% stuff
end
Or do I just have to put up with this? This may not look like an ugly example, but there are some ugly lines I have which involve comparing a number of variables against the same set of parameters. I've considered setting up a function to which I can pass an array of variables and a set of conditions to check them against, but I'd prefer a syntax-related solution. Thanks in advance.
1 Kommentar
Sean de Wolski
am 20 Mai 2015
in MATLAB, ~ is the negation not ! like in C.
Akzeptierte Antwort
Weitere Antworten (1)
Sean de Wolski
am 20 Mai 2015
Bearbeitet: Sean de Wolski
am 20 Mai 2015
Frankly, I like the way you have it. It's clear to read:
if all([x(:); y(:); z(:)])
%etc
end
would also work but is less readable.
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!