a = b<0
9 次查看(过去 30 天)
显示 更早的评论
What is the meaning of this line?
...
a = b<0;
c = 3*a;
...
Is a IF condition?
0 个评论
采纳的回答
Voss
2023-1-22
编辑:Voss
2023-1-22
a = b<0;
checks if b is less than 0 and stores the result in a.
If b is a scalar: if b is less than 0, then a will be true; otherwise a will be false.
If b is a non-scalar array: a will be a logical array the same size as b, with each element being true or false, depending on whether the corresponding element in b is less than 0 or not.
Examples:
b = 2;
a = b<0 % false
b = -2;
a = b<0 % true
b = 0;
a = b<0 % false
b = randn(3) % non-scalar array
a = b<0 % 3-by-3 logical array, true where b<0 and false elsewhere
Then the next line
c = 3*a;
multiplies a by 3 and stores it in c, so c will be an array the same size as a, with the value 3 where a is true and the value 0 where a is false.
Examples:
b = 2;
a = b<0; % false
c = 3*a
b = -2;
a = b<0; % true
c = 3*a
b = 0;
a = b<0; % false
c = 3*a
b = randn(3) % non-scalar array
a = b<0 % 3-by-3 logical array, true where b<0 and false elsewhere
c = 3*a
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!