Polyfit on a 3d plot

102 views (last 30 days)
Fabio Carton
Fabio Carton on 11 May 2022
Commented: Fabio Carton on 12 May 2022
Hello everyone,
i'm trying to create a matrix containing the coefficient of a 3rd degree polyfit on each rows with the shape 13000x4.
For that, i have the following data shape:
Time : 13000x1 %time corresponding actually to the number of rows
Position : 13000x4 %the position is just for the sake of the plot (position of the sensor in reality). The four columns have each a constant value over the 13000 rows
Displacement: 13000x4 %measurement in global Z of four sensors.
(see image attached) My purpose is to "slice" the 3d plot at every time "t" (13000 times) and apply a polyfit on the 4 red cross (x = position, y = displacement) in order then to evaluate the curve to obtain the 2 yellow points. Ultimaly, i want to recreate a line out of the yellow points.
Up to now, i was only able to extract a few sets of points (red crosses) at a given time manually. I don't know how to reshape the matrix without creating 13000 submatrix and names associated.
I hope that my explanations are clear enough and that you can give me a little help on that one.
Thanks in advance

Accepted Answer

dpb on 11 May 2022
That's fairly simple, just loop over the displacement array with a set of the position data to compute that set of coefficients and evaluate them where needed.
nD=size(D,1); % the important size of D
NewD=zeros(nD,2); % for the new points -- could augment D instead but this is simpler
for i=1:nD % iterate over rows of D
b{i}=polyfit(P(1,:),D(i,:),3); % compute polyfit coefficients for each (hold in cell array)
NewD(i,:)=polyval(b{i},newP); % evaluate and save new positions
leaves you with the additional data points at the two positions -- just draw them directly or you could add to the original D array...
  1 Comment
Fabio Carton
Fabio Carton on 12 May 2022
Thank you very much!! it was pretty much what i was looking for.
I acknowledge that it can appear easy but i'm still a learner and some loops are still tricky for me.
All the best

Sign in to comment.

More Answers (0)


Find more on Graphics Performance 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!

Translated by