matlab - Operands to the || and && operators must be convertible to logical scalar values -
i have simple problem i'm looking fast implementation in matlab. have array of values, let's say:
= floor(rand(5,5).*255)
i have sized threshold array, let's it's:
a_thresh = floor(rand(5,5).*255)
for values within a
if 0.5x smaller corresponding value in a_thresh
want output 0 - 1.2x value in a_thresh
should set zero, i.e.:
a(a < a_thresh.*0.4) = 0 a(a > a_thresh.*1.2) = 0
for values between 0.4x , 0.5x , 1.0x , 1.2x want proportional amount , else between 0.5 , 1.0 want use value of a
unaltered. thought use following:
a(a>= a_thresh .* 0.4 && <a_thresh.* 0.5) = ((a - a_thresh.*0.4)/(a_thresh.*0.5 a_thresh.*0.4)) .* a;
however, error says:
operands || , && operations must convertible logical scalar values
any advice on how solve this? use loops , trivial, want keep code vectorized.
the thing &&
can operate on scalars, whereas &
can operate on arrays well. should change &&
&
make work (you can read more in this question).
update:
regarding second problem described in comment: number of elements on left different because you're using indices (selecting elements), , on right you're working entire matrix a
, a_thresh
.
you need use indices in both sides, suggest storing in variable , use array subscript, along these lines:
idx = (a >= a_thresh*0.4 & < a_thresh*0.5); a(idx) = ((a(idx)-a_thresh(idx)*0.4) ./ (a_thresh(idx)*0.5*a_thresh(idx)*0.4)) .* a(idx);
i'm not sure if calculation correct, i'll leave check.
Comments
Post a Comment