Fitting Polynomial Curve for 4,000 curves.

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 Comments

What if it were only two? Would you still be stuck?
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.
Question was whether you've conceptually having a problem with how to do anything multiple times or just overwhelmed by the numbers...

Sign in to comment.

Answers (2)

Stephen23
Stephen23 on 4 Feb 2016
Edited: Stephen23 on 4 Feb 2016
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);
dpb
dpb on 4 Feb 2016
Edited: dpb on 4 Feb 2016
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));

Categories

Asked:

on 4 Feb 2016

Commented:

on 4 Feb 2016

Community Treasure Hunt

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

Start Hunting!