Logic conditions are not equal in if statements

3 次查看(过去 30 天)
Hello.
I am having problems understanding logical expressions and how they differ sometimes, or why certain things work or not.
% Method 1
if (size(LPData.maty)==size(LPData.matx))
disp('A')
else
disp('B')
error('Error occured.\Vital Matrices matx and maty do not agree.\nCheck for different number of columns.')
end
% Method 2
if ~(size(LPData.maty)==size(LPData.matx))
disp('C')
error('Error occured.\Vital Matrices matx and maty do not agree.\nCheck for different number of columns.')
end
Maty and matx are matrices of different sizes in this case. I understand why, in the first case, this should trigger the error message, because the two sides of the logical array (size(LPData.maty)==size(LPData.matx)) are not equal. Hence, the statement under "else" is triggered.
But why doesn't the same happen in the second case? As I understand it, the condition is not fulfilled, therefore wrong, and the path under "C" should be triggered.
I know I could just use method 1 to achieve what I want, but I'd like to get rid of the imo unhandy way of using else, instead of just using hethod 2.
Would method 2 even be possible in theory?
Could someone clarify this?
Thank you.
Stay healthy,
Claudius Appel

采纳的回答

madhan ravi
madhan ravi 2020-6-3
"if expression, statements, end evaluates an expression, and executes a group of statements when the expression is true. An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false.”
From the documentation.
  2 个评论
Claudius Simon Appel
编辑:Claudius Simon Appel 2020-8-29
So, as I understand it, you can't filter for false statements directly, since a false statement is zero.
Stephen23
Stephen23 2020-8-29
编辑:Stephen23 2020-8-29
"So, as I understand it, you can't filter for false statements directly, since a false statement is zero."
No, that is not what it means. You are conflating the truthiness of the elements with the truthiness of the expression.
That line from the documentation explains that all elements must be true in order for the expression to be considered true. Lets take a simple example of two different matrices with sizes 2x3 and 2x5, then your code does this:
~([2,3]==[2,5])
~[true,false]
[false,true]
Are all of the elements true ? No, they are not, so the expression is not considered to be true.
If the two matrices (or arrays with the same number of dimensions) share one or more dimensions with the same size, then your expression will be considered false. Only in the case that all dimensions differ will your expression will be considered true, because the final logical vector all of its elements will be true (and so the expression will be considered true, just as the documentation states). E.g. 2x3 and 4x5 matrices:
~([2,3]==[4,5])
~[false,false]
[true,true]
Tip: I suspect that you actually wanted to check if the array sizes were not the same, in which case the most robust approach is to use isequal like this:
~isequal(size(A),size(B))
Note that your code will throw an error if the two arrays have a different number of dimensions.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by