# Operands to the logical and (&&) and or (||) operators must be convertible to logical scalar values. What is my mistake ?

35 visualizaciones (últimos 30 días)
FURKAN AVCI el 21 de Ag. de 2021
Respondida: Steven Lord el 21 de Ag. de 2021
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 comentarioMostrar NingunoOcultar Ninguno
Walter Roberson el 21 de Ag. de 2021
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.

Iniciar sesión para comentar.

### Respuestas (3)

Star Strider el 21 de Ag. de 2021
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 comentariosMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos

Iniciar sesión para comentar.

TADA el 21 de Ag. de 2021
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
##### 0 comentariosMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos

Iniciar sesión para comentar.

Steven Lord el 21 de Ag. de 2021
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
##### 0 comentariosMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Numbers and Precision en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by