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.

回答(3 个)

Star Strider
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.
.

TADA
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
tf1 = 1×6 logical array
0 0 1 1 1 1
tf2 = sales < 1000000
tf2 = 1×6 logical array
1 1 1 1 1 0
the problem is you can't run logical operators (&& and ||) on vectors, only on scalars.
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:
  1. solve this issue and revert it to a scalar
  2. do these comparisons on sales inside a loop
  3. 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
bonusRate = 1×6
0 0 0.0050 0.0050 0.0050 0.0075

Steven Lord
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)
theRate = 1×5
0 0.0050 0.0050 0.0075 0.1000

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by