I ran a quick test to see how if
deals with multiple input values. It appears that the default behavior is to apply and
to the collection of values, but I couldn't find any documentation. Can anyone confirm or provide a counterexample?
>> if([1,1,1]) disp(sprintf('hi'));end;
hi
>> if([1]) disp(sprintf('hi'));end;
hi
>> if([1,1,0]) disp(sprintf('hi'));end;
EDIT: to clarify, I don't intend to try to use this "feature," but wanted to be sure I knew how erroneous input would be handled. Suppose, for example, your code read if(my_function)
and the (badly written) my_function
usually returns a single value but occasionally returns multiple values. Good practice, of course , would parse the returned values appropriately and feed a single value to if
.
I don't find any practical reason to create an if
statement which depends on anything but a scalar.
Regarding what's done in MATLAB practically?
You may assume MATLAB applies the function all
on the input of the if
statement. This will hold as intuition given the array is non empty.
For example, if we have array - array4Example
which is not empty, the statement if
on the array - if(array4Example)
is equivalent of the statement if on the scalar - if(all(array4Example(:)))
.
This matches the documentation if the if
function.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.