Polyfit with weights for not allowed points
Show older comments
I have a multiple matrices that I'm trying to fit with multiple polyfit functions (see picture). I'd like to combine the matrices in one single matrix and then fit them with one polyfit function. The issue is that some columns of the matrices are empty (hence the curves in the picture don't cover the whole range), therefore I cannot combine all them together.
I hope this is clear. I would really appreciate some help. Thanks!

Answers (2)
There is nothing to be gained by combining the data, since polyfit will only process one pair of vectors (x,y) per call.
4 Comments
Matt J
on 18 Dec 2016
alberto tonizzo commented:
Thank you for your answer, but I'm a little confused. I'm now doing a polyfit like this (for each colored curve in the figure): C = polyfit(X,Y,4); where X and Y are [330x24] matrices.
Are you saying that the polyfit I'm doing using X and Y matrices are wrong? And can I do a polyfit with cell array?
Thanks again for your input!
The shape of X and Y is irrelevant. You will get the same polynomial coefficient fits regardless of the size of X and Y, as long as size(X)=size(Y).
The point is that polyfit does not perform multiple fits, if that's what you were hoping. You will have to use a loop of polyfit calls if you want to fit multiple polynomials to multiple (X,Y) data sets.
alberto tonizzo
on 18 Dec 2016
You can turn any matrix into a row vector as follows
rowvector=matrix(:).';
or
rowvector=reshape(matrix,1,[]);
Jos (10584)
on 18 Dec 2016
You can simply concatenate the data into a single vector
% xk, yk are your data sets. concatenate them into a single vector
x_combined = [x1 x2 x3] % assuming sets are row vectors
y_combined = [y1 y2 y3]
p = polyfit(x_combined, y_combined, ...)
1 Comment
alberto tonizzo
on 18 Dec 2016
Categories
Find more on Creating and Concatenating Matrices 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!