From: J B on
I want to change values of vector 'a' that are less than 0 to a(n)+10. This code does produce the correct values, but for the values that dont meet the criterion 'a(n)<0' a zero is inserted into the vector b instead of the original value. Any ideas?

for n=1:length(a)
if a(n)<0
b(n)=a(n)+10
end
end
From: Jan Simon on
Dear J B!

> I want to change values of vector 'a' that are less than 0 to a(n)+10. This code does produce the correct values, but for the values that dont meet the criterion 'a(n)<0' a zero is inserted into the vector b instead of the original value. Any ideas?
>
> for n=1:length(a)
> if a(n)<0
> b(n)=a(n)+10
> end
> end

Some ideas:
for n=1:length(a)
if a(n) < 0
b(n) = a(n)+10
else
b(n) = a(n);
end
end

Faster:
b = a;
for n=1:length(a)
if a(n) < 0
b(n) = a(n)+10
end
end

Efficient:
b = a;
match = (b < 0);
b(match) = b(match) + 10;

Good luck, Jan
From: J B on
Thanks Jan!