Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values. What is my mistake ?
조회 수: 12 (최근 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!