Fitting Polynomial Curve for 4,000 curves.
Show older comments
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
dpb
on 4 Feb 2016
What if it were only two? Would you still be stuck?
John D'Errico
on 4 Feb 2016
Hint:
help for
todd feldman
on 4 Feb 2016
dpb
on 4 Feb 2016
Question was whether you've conceptually having a problem with how to do anything multiple times or just overwhelmed by the numbers...
Answers (2)
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 Comment
todd feldman
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
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!