Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values. What is my mistake ?
15 次查看(过去 30 天)
显示 更早的评论
if ((sales >= 500000) && (sales < 1000000) )
bonusRate = 0.005;
elseif ((sales >= 1000000) && (sales < 2000000) )
bonusRate = 0.0075;
elseif ((sales >= 2000000) )
bonusRate = 0.01;
end
Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values.
1 个评论
Walter Roberson
2021-8-21
What output do you want to have if
sales = [600000 600000 1200000 2400000]
Keeping in mind that in
bonusRate = 0.005;
that the right hand side is a scalar.
回答(3 个)
Star Strider
2021-8-21
If thie posted code is in a loop and ‘sales’ is a scalar at each iteration, that error would likely not be thrown.
salesv = randi([1E+5 2E+6], 10, 1);
for k = 1:numel(salesv)
sales = salesv(k);
if ((sales >= 500000) && (sales < 1000000) )
bonusRate(k) = 0.005;
elseif ((sales >= 1000000) && (sales < 2000000) )
bonusRate(k) = 0.0075;
elseif ((sales >= 2000000) )
bonusRate(k) = 0.01;
end
end
figure
scatter(salesv, bonusRate)
grid
xlabel('Sales')
ylabel('Bonus Rate')
If ‘sales’ is a vector, an easier way to write that would be:
sales = salesv;
bonusRate = 0.005.*((sales >= 500000) & (sales < 1000000)) + 0.0075.*((sales >= 1000000) & (sales < 2000000)) + 0.01.*((sales >= 2000000));
figure
scatter(sales, bonusRate, 's')
grid
xlabel('Sales')
ylabel('Bonus Rate')
Use single operators for vectors, and ‘short circuit’ operators for scalars.
.
0 个评论
TADA
2021-8-21
your variable sales is probably a vector (or matrix)
then what you get from your comparison operators ">=" and "<" are logical vectors (or matrices)
sales = (1:2:11)*1e5;
tf1 = sales >= 500000
tf2 = sales < 1000000
you can use bit-wise logical operations which operate on each element of the vectors separately, such as &, |, ~ and functions such as any and all, and finding which elements follow these rules using find or logical indexing, but the usecases aren't always similar.
I'd check why and if sales should be a vector, then see whether you want to treat sales differently:
- solve this issue and revert it to a scalar
- do these comparisons on sales inside a loop
- perform all the comparisons using & and | instead of the if statements and &&\|| operators and calculate the bonus rate vector using a logical index (the reason to use matlab)
bonusRate = zeros(size(sales));
bonusRate((sales >= 500000) & (sales < 1000000) ) = 0.005;
bonusRate((sales >= 1000000) & (sales < 2000000) ) = 0.0075;
bonusRate(sales >= 2000000) = 0.01
0 个评论
Steven Lord
2021-8-21
Use discretize.
% If sales are in the range [0, 500000) the bonus rate is 0
% If sales are in the range [500000, 1000000) the bonus rate is 0.005
% etc
% If sales are in the range [2000000, Inf] the bonus rate is 0.1
rateEdges = [0, 500000, 1000000, 2000000, Inf];
bonusRates = [0, 0.005, 0.0075, 0.1];
sales = [100000, 600000 600000 1200000 2400000];
theRate = discretize(sales, rateEdges, bonusRates)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!