http://www.mathworks.com/matlabcentral/newsreader/view_thread/280857
MATLAB Central Newsreader  Loop to evaluate consecutive values in array
Feed for thread: Loop to evaluate consecutive values in array
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 29 Apr 2010 20:20:24 +0000
Loop to evaluate consecutive values in array
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280857#740637
M.E.L.
I'm new to Matlab and still unsure as to how to create loops. I want to make a loop that will run through each value in the array and compare consecutive values. If the difference between consecutive values is greater than pi, I want to add 2*pi to the second value. If the difference between consecutive values is less than pi I want to subtract 2*pi from the second value. How might I go about doing this?<br>
<br>
Thank you!

Thu, 29 Apr 2010 20:32:19 +0000
Re: Loop to evaluate consecutive values in array
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280857#740642
dpb
M.E.L. wrote:<br>
> I'm new to Matlab and still unsure as to how to create loops. I want to <br>
> make a loop that will run through each value in the array and compare <br>
> consecutive values. If the difference between consecutive values is <br>
> greater than pi, I want to add 2*pi to the second value. If the <br>
> difference between consecutive values is less than pi I want to <br>
> subtract 2*pi from the second value. How might I go about doing this?<br>
<br>
You don't need loops most of the time in Matlabthat's the beauty of it...<br>
<br>
x = some_values;<br>
dx = diff(x); % sequential differences<br>
idx1 = find(dx>pi);<br>
x(idx1) = x(idx1+1)+2*pi;<br>
<br>
idx2 = find(dx<pi);<br>
x(idx2) = x(idx2+1)2*pi;<br>
<br>
You may have to do this repetitively, however...<br>
<br>


Thu, 29 Apr 2010 20:48:04 +0000
Re: Loop to evaluate consecutive values in array
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280857#740646
Roger Stafford
"M.E.L. " <schrodingers.lyon@gmail.com> wrote in message <hrcpm8$def$1@fred.mathworks.com>...<br>
> I'm new to Matlab and still unsure as to how to create loops. I want to make a loop that will run through each value in the array and compare consecutive values. If the difference between consecutive values is greater than pi, I want to add 2*pi to the second value. If the difference between consecutive values is less than pi I want to subtract 2*pi from the second value. How might I go about doing this?<br>
> <br>
> Thank you!<br>
<br>
There is a matlab function called 'unwrap' which will do that whole job for you in one line.<br>
<br>
xnew = unwrap(xold);<br>
<br>
Roger Stafford

Thu, 29 Apr 2010 21:02:04 +0000
Re: Loop to evaluate consecutive values in array
http://www.mathworks.com/matlabcentral/newsreader/view_thread/280857#740651
us
"M.E.L. " <schrodingers.lyon@gmail.com> wrote in message <hrcpm8$def$1@fred.mathworks.com>...<br>
> I'm new to Matlab and still unsure as to how to create loops. I want to make a loop that will run through each value in the array and compare consecutive values. If the difference between consecutive values is greater than pi, I want to add 2*pi to the second value. If the difference between consecutive values is less than pi I want to subtract 2*pi from the second value. How might I go about doing this?<br>
> <br>
> Thank you!<br>
<br>
just want to make sure you'll also look at these<br>
<br>
help for;<br>
help while;<br>
help end;<br>
<br>
us