|
|
Loop to evaluate consecutive values in array
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!
|
|
0
|
|
|
|
Reply
|
M
|
4/29/2010 8:20:24 PM |
|
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...
--
|
|
0
|
|
|
|
Reply
|
dpb
|
4/29/2010 8:32:19 PM
|
|
"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
|
|
0
|
|
|
|
Reply
|
Roger
|
4/29/2010 8:48:04 PM
|
|
"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
|
|
0
|
|
|
|
Reply
|
us
|
4/29/2010 9:02:04 PM
|
|
|
3 Replies
362 Views
(page loaded in 0.026 seconds)
Similiar Articles: Loop to evaluate consecutive values in array - comp.soft-sys ...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. looping CSV.foreach?? - comp.lang.ruby... comp.lang.ruby Skip lines in a for loop - comp.soft-sys.matlab A for loop can run over any array, not just a consecutive array. ... sys.matlab Then once I select those values ... Find minimum value in array - comp.soft-sys.matlabAnd inside that for loop is where you want ... Get minimum of the sum of 12 consecutive numbers in an array of 48 ... ... Finding the two closest array values to some ... Copying rows in a two dimensional array. - comp.lang.ada ...I'm working with a two dimensional array of floating point values. ... the procedure would see it as an array of 20 consecutive ... private static void loop_transpose(int ... How to select and sum elements in an array fast? - comp.soft-sys ...I can do a for loop S = zeros(length(B),1 ... those elements within ... sys.matlab ... consecutive array ... there are 2 Inf and 2 NaN values found in the array, then the ... memcpy from within matlab - comp.soft-sys.matlab... neck given that I use it very often in a loop, is ... Thank you for the advise, No , they are NOT consecutive arrays ... exposed, which would at least allow one to evaluate ... replicating MS Excel percentrank formula - comp.soft-sys.matlab ...... store sorted array data_sorted = data(i,:); %loop through for i = 1:length(data) % find number of values less than ... Extrapolation will % evaluate to NaN ... Compare numbers - comp.soft-sys.matlab... have variable "y" y = 2 and I have cell array "k ... In that case, I suppose you'll need a loop to convert ... Less Than Tips and advice on how to help your child evaluate ... instead of For loop - comp.soft-sys.matlabThanks > Maxx A for loop can run over any array, not just a consecutive array. ... created a simple for loop where the values of a number ... instead of For loop ... how to transpose large matrix? - comp.unix.shell> > Are you assuming that zero values comstitute "sparse" elements of the array? ... corresponding to > "strips" of n consecutive ... times faster than a "while read" loop ... Loop to evaluate consecutive values in array - comp.soft-sys ...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. Loop to evaluate consecutive values in array - Newsreader - MATLAB ...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. 7/22/2012 4:06:23 PM
|
|
|
|
|
|
|
|
|