Why doesn't [] ~=1 yield logical one?

4 views (last 30 days)
I'm using matlab r2012B, because the computer is old and disconnected from internet and it runs my experiment and is not to be updated.
I have the following:
a = [];
if a ~= 1
disp('a~=1')
end
Now i ask myself, will the message 'a~=1' be printed? the answer is obviously yes. Why? because the expression 'a~=1' is clearly True. It is True that [] is not equal to 1. one is the number 1, and the other, is NOT the number 1. If I
If this threw an error, I would accept that. That seems reasonable.
Therefore, there must be some reason that this doesn't behave like I expect. What is that reason? Why does it behave the same way for a = 'string' (which evaluates to 0, makes sense) or a = {} (which throws an error, also reasonable).

Accepted Answer

John D'Errico
John D'Errico on 22 May 2021
Edited: John D'Errico on 23 May 2021
This has always been true as I recall, probably so since the mid-1980's, when I learned MATLAB.
Note that the result is an EMPTY arrray. Suppose we had performed the test:
a = 1;
x = 0:3;
x ~= a
ans = 1×4 logical array
1 0 1 1
We have compared the scalar value a to EVERY element in the vector x. 3 of the 4 values were not equal, and the other is equal. So we get a completely expected result, true where x is not equal to 1.
The same applies to a vector of length 0, thus an empty vector. Try this comparison:
y = [];
y ~= a
ans = 0×0 empty logical array
Again, every element in y is compared to a. Since there are no elements in y, the result is an empty vector.
Your problem is in how you view an empty array. It is NOT a scalar. That is
whos a x y
Name Size Bytes Class Attributes a 1x1 8 double x 1x4 32 double y 0x0 0 double
We see that y is not the same size as a. So while the comparison to x performs a scalar expansion, the same comparison to y results in a scalar expansion of a, to be the same size and shape as y: EMPTY.
We cannot compare a scalar value to y (an empty array) and expect a result that is either true or false. By the way, this is why the function isempty has been around since the dark ages.
Now, how does this work for a string? I'm not certain what you intended there.
If we do this:
str = '';
str == '1'
ans = 0×0 empty logical array
Then we get the same behavior with an empty string as with an empty array. If we try to compare a non-empty string to an ampty array, it will throw an error.
str = 'string';
str == ''
Error using ==
Arrays have incompatible sizes for this operation.
As you can see, I cannot compare a string VECTOR to an empty array, so it throws an error. I cannot expand an empty array. It is NOT a scalar.
Again, I think you are viewing an empty array as if it is a scalar. It is not so. Likewise, you cannot compare a scalar to a cell array, empty or NOT! But that is not the same issue. Comparisons are not defined like this for a cell array:
{} == 1
Operator '==' is not supported for operands of type 'cell'.
  4 Comments
Paul
Paul on 23 May 2021
In r2012b, would the results from my code snippet be any different? It seems to me that
y ~= a
would still return empty in accordance with your third bullet, and
y ~= x
would presumably throw an error because it's not covered by bullets 1-3.
I realize that based on the OP's restriction to r2012b I shouldn't have brought post-r2012b features into the discussion. I'm just curious if I understand what that older behavior is.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!