Hi!
I have a situation in my code like this:
a = b+c ~= d/e;
where a, b, c, d and e are real numbers, but I don't know what this expression means...
Can you help me, please?
Thank you! :D

 Akzeptierte Antwort

Voss
Voss am 4 Mär. 2023

0 Stimmen

a = b+c ~= d/e;
for scalars b, c, d, and e, is the same as
if b+c ~= d/e
a = true;
else
a = false;
end

6 Kommentare

Note that ~= has a low priority, so the code is interpreted as
(b+c) ~= (d/e)
Laura Costa Pereira Miranda
Laura Costa Pereira Miranda am 6 Mär. 2023
Thank you Voss and Walter for the answers!
But, in this case, if a is true (or false), a will have just this binary value? Because, when I display the value of a, this variable assumes a real value.
I'm still confuse about it.
And thank you a million for the answers! <3
Yes, a will be either true or false. a will be a logical scalar.
When you display a, true is represented as a 1 and false is represented as a 0:
a = true
a = logical
1
a = false
a = logical
0
But notice they are of class logical, not double (or some other numeric class).
However, if you use a in a calculation, the value 0 or 1 will be used:
a = true;
result = a+4 % true+4 is interpreted as 1+4
result = 5
a = false;
result = false*8 % false*8 is interpreted as 0*8
result = 0
and result is of class double (the default numeric class):
class(result)
ans = 'double'
Laura Costa Pereira Miranda
Laura Costa Pereira Miranda am 6 Mär. 2023
Oh! I understand now! It makes sense :D
Thanks a million!!
Voss
Voss am 6 Mär. 2023
You're welcome!
By the way, in the context of if or while then an expression is considered true if the expression evaluates to something that is all non-zero (if it evaluates to contain nan then you will get an error)
if -3:2:5; disp('yep okay'); else disp('nope'); end
yep okay
if -4:2:4; disp('yep okay'); else disp('nope'); end
nope
The first one is completely non-zero but the second one contains at least one zero.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by