From: J B on 14 Feb 2010 06:37 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 14 Feb 2010 07:03 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 14 Feb 2010 10:09 Thanks Jan!
|
Pages: 1 Prev: using trap to numerically integrate between bounds Next: plot if FOR loop |