Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Loop to evaluate consecutive values in array

Subject: Loop to evaluate consecutive values in array

From: M.E.L.

Date: 29 Apr, 2010 20:20:24

Message: 1 of 4

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?

Thank you!

Subject: Loop to evaluate consecutive values in array

From: dpb

Date: 29 Apr, 2010 20:32:19

Message: 2 of 4

M.E.L. wrote:
> 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?

You don't need loops most of the time in Matlab--that's the beauty of it...

x = some_values;
dx = diff(x); % sequential differences
idx1 = find(dx>pi);
x(idx1) = x(idx1+1)+2*pi;

idx2 = find(dx<-pi);
x(idx2) = x(idx2+1)-2*pi;

You may have to do this repetitively, however...

--

Subject: Loop to evaluate consecutive values in array

From: Roger Stafford

Date: 29 Apr, 2010 20:48:04

Message: 3 of 4

"M.E.L. " <schrodingers.lyon@gmail.com> wrote in message <hrcpm8$def$1@fred.mathworks.com>...
> 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?
>
> Thank you!

  There is a matlab function called 'unwrap' which will do that whole job for you in one line.

 xnew = unwrap(xold);

Roger Stafford

Subject: Loop to evaluate consecutive values in array

From: us

Date: 29 Apr, 2010 21:02:04

Message: 4 of 4

"M.E.L. " <schrodingers.lyon@gmail.com> wrote in message <hrcpm8$def$1@fred.mathworks.com>...
> 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?
>
> Thank you!

just want to make sure you'll also look at these

     help for;
     help while;
     help end;

us

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us