Pergunta

if tf < max(arr) Vs. if all(tf < max(arr))

  1. what is the difference between them
  2. which one is recommended.
  3. are they exactly equivalent?

I get an error when using matlab coder with the first one though!

Edit

I have

if (xf-x0 < small_dist)&&(v0 <= min(vmax0, vmaxf))

and considered to change it to

leftCmpStatement = all((xf-x0 < small_dist));
rightCmpStatement = all((v0 <= min(vmax0, vmaxf)));
if leftCmpStatement && rightCmpStatement

because matlab coder complaining not that each element is a 1X1 double variable

Foi útil?

Solução

They are the same if numel(max(arr)) is 1. If not then if tf < max(arr) will only consider the first maximum. But since all the max(arr) elements will have the same value it will still work.

However if you used a function that could have returned different values, such as simply if tf < arr vs if all(tf < arr) then they are different. if all(tf < arr) works as expected, if must be smaller then every element of arr. HOWEVER if tf < arr becomes something like if [true false true false false...] and (thanks to Amro for pointing this out) Matlab will only conider this true if it is both non-empty and all the elemnets are non-zero. So in my made up example it will be false. So the moral of the story is don't give if a multi-element matrix in Matlab. So I would go for if all(tf < max(arr)) just to be consistent with that rule.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top