Place polyfit values in a matrix using a loop

3 ビュー (過去 30 日間)
Nathan Kennedy
Nathan Kennedy 2018 年 2 月 8 日
コメント済み: Star Strider 2018 年 2 月 13 日
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 件のコメント
John D'Errico
John D'Errico 2018 年 2 月 8 日
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.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2018 年 2 月 8 日
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 件のコメント
Nathan Kennedy
Nathan Kennedy 2018 年 2 月 13 日
My mistake, thank you for this!
Star Strider
Star Strider 2018 年 2 月 13 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by