Logic conditions are not equal in if statements
Show older comments
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
1 Comment
Stephen23
on 3 Jun 2020
"Could someone clarify this?"
Accepted Answer
More Answers (0)
Categories
Find more on Electrophysiology in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!