Logical operator not evaluating correctly
1 次查看(过去 30 天)
显示 更早的评论
Given the provided array X, create an array Y that is:
equal to 0 if the corresponding element of X is between -5 and +5, inclusive
equal to -1 if the corresponding element of X is less than -5
equalt to +1 if the corresponding element of X is greater than 5
Here is what I have:
Y = (~(X >= -5 & X <= 5) | (-1*(X < -5)) | (X>5)).
Why is the X < -5 not evaluationg to -1?
0 个评论
采纳的回答
Star Strider
2023-1-26
Only two comparisons are actually required here —
X = linspace(-7, 7, 15);
Y = -(X < -5) + (X > 5);
Check_Result = [X; Y]
.
0 个评论
更多回答(2 个)
Walter Roberson
2023-1-26
A|B is an or test and returns a logical value -1 is nonzero which is considered true, and since you or everything together you are going to end up with logical true for those entries.
Try adding the results of the computation
0 个评论
Les Beckham
2023-1-26
I would do this in separate steps.
X = randi([-20, 20], [1 15])
Y = zeros(size(X));
Y(X < -5) = -1;
Y(X > 5) = 1
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!