debugging - Why does my Matlab code not work correctly? -
my code
b(abs(b(1:3:length(b))) > 0.75) = 0.75
what it's supposed do:
b1 = b(1:3:end); b1(abs(b1)>0.75) = 0.75; b(1:3:end) = b1;
how these 2 not same?
the indexing part b(1:3:end)
returns short vector of zeros , ones, change i
-th entry of b
(for i
in first third-ish of b
) 0.75
if absolute value of 3*i + 1
-th entry greater 0.75
.
for example:
b = [-0.684; 0.941; 0.914; -0.029; 0.6; -0.716; -0.156; 0.831; 0.584; 0.919]; b_index = abs(b(1:3:length(b)))>0.75
would return
b_index = 0 0 0 1
and b(b_index) = 0.75
change 4th entry of b
0.75
.
one way of doing one-liner is
b(1:3:end) = b(1:3:end).*(abs(b(1:3:end))<=0.75)) + 0.75*((b(1:3:end)>0.75));
but think three-liner bit clearer.
Comments
Post a Comment