Comments on: Empty Arrays with Flow of Control and Logical Operators https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/?s_tid=feedtopost Loren Shure is interested in the design of the MATLAB language. She is an application engineer and writes here about MATLAB programming and related topics. Mon, 07 Mar 2011 12:07:39 +0000 hourly 1 https://wordpress.org/?v=6.2.2 By: Loren https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-32083 Mon, 07 Mar 2011 12:07:39 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-32083 Sundar-

isnan is an elementwise function, so it’s returning arrays the same size and as x and y, not scalars, which is why && is complaining. You may want to judiciously use the function any with ~isnan and collapse the right dimensions to get a scalar before doing the &&.

–Loren

]]>
By: Sundar https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-32082 Sat, 05 Mar 2011 11:47:00 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-32082 Hi
I have a problem with using these short-circuit operators.
Say, x = NaN; x(:,:,2) = NaN; x(:,:,3) = NaN;
y = 1; y(:,:,2) = 2; y(:,:,3) = 3;
if isnan(x(:,:,:)) == 0 && isnan(y(:,:,:)) == 0
% Execute functions
end

I want to execute certain functions only if I have at least one non-NaN value in x and at least one NaN value in y. This means that the loop should end in the above example because all values of x is NaN.
But I get this error: ‘Operands to the || and && operators must be convertible to logical scalar values’

Am I doing something wrong here?

Thanks

]]>
By: Loren https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-31489 Fri, 09 Jul 2010 21:59:00 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-31489 Vinz-

You might try dbstop if error so you can see the link of code and the values where the issue is exposed.

If that doesn’t help, please contact technical support (link on right of blog) with enough information to reproduce the issue so they can help you.

–Loren

]]>
By: vinz https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-31481 Thu, 08 Jul 2010 19:38:23 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-31481 I am unable to resolve the below error. I am unable to figure out where the problem is becuase; Iam not using any || and && in my program nor combinations of if and while statements.
ERROR:
??? Operands to the || and && operators must be convertible to logical scalar values.

Error in ==> fzero at 333
elseif ~isfinite(fx) || ~isreal(fx)

Error in ==> BRALHX_Condenser_Equations at 69
Fdel=fzero(Fun,1.4766e-007,options);

It will really helpful if I understand more about this error with respect to fzero function.

Cheers
Vinz

]]>
By: LyVe https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30845 Mon, 23 Nov 2009 13:02:15 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30845 This behavior has tripped me up quite a few times now.

Although the rules make sense individually, they are inconsistent*.

What trips me up is when I use an if statement and add a condition later. So I start with

if A then B

and then decide that C is a condition too, so I turn it into

if A & C then B

only to have things crash if A is empty – which was previously not a problem.

I find it counterintuitive that adding code concerning C changes the way A is treated. This makes error-checking a lot more difficult, until you learn to take countermeasures.

I appreciate being able to use empty as false: if the variable has no value, it definitely doesn’t have the right one. This is very intuitive for me. However, I’m now considering teaching myself not to use this shortcut (and adding a short-circuited ~isempty instead), because it might trip me up later, unless I can learn to remember that when adding a condition to an if or while, I also need to add a short-circuited isempty…

I’d really like for the short-circuiting operators to evaluate in the same way that if and while do; not just for empties but also for arrays and such. Is there a particular reason that they require scalar values? Could we get ||| and &&& (for instance) to short-circuit for arrays (including empties)?

* I must admit that it’s quite probable that there is no consistent way to handle this, other than never accepting [] where a logical is expected. This would be more robust for the user (smaller chance of a small change causing large problems), but less convenient. So maybe all of this simply can’t be helped.

]]>
By: Loren https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30819 Thu, 19 Nov 2009 11:52:44 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30819 Marcel-

Please feel free to use the link at the right to make this enhancement suggestion. I personally don’t mind instead using another try-catch inside the catch block. I find the nesting clarifies my thinking.

–Loren

]]>
By: Marcel Zwiers https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30817 Thu, 19 Nov 2009 10:13:40 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30817 It’s not exactly on topic but what I miss in the try-catch syntax is a flow as in the if-elseif-end-syntax. Thus I would like to see a try-elsetry-catch block. I think it’s a very natural thing to have, no?

Best,
Marcel

]]>
By: Loren https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30793 Mon, 16 Nov 2009 13:38:27 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30793 Ben

if {} errors because the expression has to evaluate to something numeric, even if empty. Same for i. Neither one can be converted to a logical array.

–Loren

]]>
By: Loren https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30792 Mon, 16 Nov 2009 13:34:33 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30792 Ben-

Another good way to look at the behavior! Thanks.

–Loren

]]>
By: Ben https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30791 Mon, 16 Nov 2009 08:03:29 +0000 https://blogs.mathworks.com/loren/2009/11/12/empty-arrays-with-flow-of-control-and-logical-operators/#comment-30791 Matt/Loren,

Defining all([])=true has the additional advantage that all([A;B])=(all(A)&all(B)) holds when either array is empty.

The older versions used to generate an error for “if []” didn’t they? (although “if nan”, “if {}” and “if i” still do!) I guess that why I still prefer to write “if ~isempty(x) && all(x(:))” when it’s needed.

]]>