Place polyfit values in a matrix using a loop

Hi
I want to capture all the values from the polyfit function in a matrix when it is in a loop.
for a=1:length(x)
temp_x=x(1:a)
temp_y=y(1:a)
p(a,:) = polyfit(x,y,3); % Not working as intended
end
The polyfit function generates 4 values in a 1 row 4 column array. So I would like to end up with a matrix that has rows equal to the length of x and 4 columns containing the polyfit values for each iteration of the loop.
Please can someone advise

1 commentaire

Are you thinking this will create something like a cubic spline? Or some sort of local cubic interpolation? If so, there are better methods. Far better choices you can make.

Connectez-vous pour commenter.

 Réponse acceptée

I am not certain what you want to do, specifically because you are not doing anything with ‘temp_x’ and ‘temp_y’.
Perhaps you intend something like this:
for a=1:length(x)-3
temp_x = x(1:a+3)
temp_y = y(1:a+3)
p(a,:) = polyfit(temp_x,temp_y,3);
end
Note that to do a third-order regression, you must have at least four elements in the data vectors.

6 commentaires

p = polyfit(x,y,3);
gives a 1x4 array. If X has 50 data points, I need to have a 50x4 array.
I have a curve and I want to go along that curve in segments and find a polyfit for each segment, so I loop. I need temp_x and temp_y so it doesn't overwrite my original data.
for a=1:length(x)-3
temp_x=x(1:a+3) %Four data elements in segment for third order
temp_y=y(1:a+3) %Four data elements in segment for third order
p(a,:) = polyfit(x,y,3); % not working. Fills each column with the same number
end
‘I have a curve and I want to go along that curve in segments and find a polyfit for each segment, so I loop. ’
The problem is that in your code:
p(a,:) = polyfit(x,y,3); % not working. Fills each column with the same number
you are fitting all of (x,y) in every iteration of the loop, so the parameter estimates never change.
Try this:
seg_len = 3;
for a=1:length(x)-seg_len
temp_x = x(a:a+seg_len)
temp_y = y(a:a+seg_len)
p(a,:) = polyfit(temp_x,temp_y,3);
end
Change ‘seg_len’ to whatever you want (so long as it is at least 3 for a third-degree polynomial fit).
‘If X has 50 data points, I need to have a 50x4 array.’
That’s not possible. You can have at most ‘numel(X)-seg_len’ rows in ‘p’, unless you want to use circshift to wrap around the end of the vectors for ‘seg_len’ elements (that would likely not produce reliable results for the last three rows of ‘p’).
Nathan Kennedy
Nathan Kennedy le 12 Fév 2018
Modifié(e) : Nathan Kennedy le 12 Fév 2018
Tried your code, It gives an error
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in Polyfit_0802_1 (line 191)
p(a,:) = polyfit(temp_x,temp_y,3);
I tried it too, and it doesn’t throw an error when I run it:
x = rand(1,50);
y = rand(1,50);
seg_len = 3;
for a=1:length(x)-seg_len
temp_x = x(a:a+seg_len)
temp_y = y(a:a+seg_len)
p(a,:) = polyfit(temp_x,temp_y,3);
end
My mistake, thank you for this!
Star Strider
Star Strider le 13 Fév 2018
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by