My code
b(abs(b(1:3:length(b))) > 0.75) = 0.75
What it's supposed to do:
b1 = b(1:3:end);
b1(abs(b1)>0.75) = 0.75;
b(1:3:end) = b1;
How are these two not the same?
The indexing part b(1:3:end)
returns a short vector of zeros and ones, so will only change the i
-th entry of b
(for i
in the first third-ish of b
) to 0.75
if the absolute value of the 3*i + 1
-th entry is greater than 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
would change the 4th entry of b
to 0.75
.
One way of doing this as a 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 I think the three-liner is a bit clearer.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments