I am trying to use a logical array mask to square all the values of this array a = [1:1:2000}. The logical array mask is defined as b = a <500. How would I square those values using the mask?
a = 1:2000; %# 1 by 2000 double
b = a<500; %# 1 by 2000 logical
a_squared = a(b).^2; %# 1 by 499 double
%# logical index--^ ^-- 'dot' means element-wise operation
If you need the result to be the same size as a
(keeping a >= 500
values as is), then:
a_sq = (a .^ 2) .* (a < 500) + a .* (a >= 500);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With