I would like to fit a second order polynomial where data(1:7,1) is the x and data(1:7,i) is the y. I would like to loop through where i=2,3,4,5, and so on. I have 4,000 i in my actual data set. Any help would be great.

4 件のコメント

dpb
dpb 2016 年 2 月 4 日
What if it were only two? Would you still be stuck?
John D'Errico
John D'Errico 2016 年 2 月 4 日
Hint:
help for
todd feldman
todd feldman 2016 年 2 月 4 日
Only two? Column 1 is my x and Column 2 to 4000 is my y. I only show column 2 and 3 here as a sample.
dpb
dpb 2016 年 2 月 4 日
Question was whether you've conceptually having a problem with how to do anything multiple times or just overwhelmed by the numbers...

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

回答 (2 件)

Stephen23
Stephen23 2016 年 2 月 4 日
編集済み: Stephen23 2016 年 2 月 4 日

0 投票

ord = 2;
data = [1:7;20:23,25,26,25;21,24,23,23:26]'
num = size(data,2)-1;
cof = cell(ord+1,1);
for k = 1:num
cof{k} = polyfit(data(:,1),data(:,1+k),ord);
end
then to get the coefficients in a numeric array:
mat = cell2mat(cof);

1 件のコメント

todd feldman
todd feldman 2016 年 2 月 4 日
Thank you, I will try that.

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

dpb
dpb 2016 年 2 月 4 日
編集済み: dpb 2016 年 2 月 4 日

0 投票

Same basic solution as Stephen's above excepting doing the conversion on the front end instead of the latter and having implicit loop instead of explicit...
[nr nc]=size(data); % rows, columns in data
y=mat2cell(data(:,2:end),nr,ones(nc-1,1)); % convert to a cell array by column
poly=2; % order to fit
coeff=cellfun(@(y,poly) polyfit(data(:,1),y,poly),y, ...
'uniformoutput',false); % compute
As Stephen showed, can then convert to an "ordinary" 2D array with cell2mat or leave as cell array. If choose to convert, can simply wrap that around the cellfun call and do it all in "one swell foop".
coeff=cell2mat(cellfun(@(y,poly) polyfit(data(:,1),y,poly),y, ...
'uniformoutput',false));

カテゴリ

ヘルプ センター および File ExchangePolynomials についてさらに検索

質問済み:

2016 年 2 月 4 日

コメント済み:

2016 年 2 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by