MATLAB Answers

Adding Polyfit to a table

7 views (last 30 days)
mehdi kordi
mehdi kordi on 24 Apr 2020
Answered: Peter Perkins on 5 May 2020
Can anyone help me,
I have uploaded a table and want to add another column to it but I want to use the polyfit function as I want 1 column to return back the gradient and 1 column to return back the y-intercept.
I have has to re-add the time points which are constant for all [0.00556 0.0033 0.00138889]
My questions are - can I just use the time points as a variable e.g as "a" or something and how do I calculate the gradient using the 3 measures with against the 3 constant time point?
BodyMass PPO threemin fivemin twelvemin threetime fivetime twelvetime
________ ____ ________ _______ _________ _________ _________ __________
68.08 617 252 233 210 0.0055556 0.0033333 0.00138889
68 939 448 412 386 0.0055556 0.0033333 0.00138889
68.08 620 260 233 200 0.0055556 0.0033333 0.00138889
68 958 453 401 347 0.0055556 0.0033333 0.00138889
83.5 1214 397 362 335 0.0055556 0.0033333 0.00138889
72.5 1039 379 358 335 0.0055556 0.0033333 0.00138889
68 975 451 384 320 0.0055556 0.0033333 0.00138889
84.5 1290 398 353 314 0.0055556 0.0033333 0.00138889
71.42 975 374 333 301 0.0055556 0.0033333 0.00138889
84.5 1278 417 370 319 0.0055556 0.0033333 0.00138889
71.42 981 403 333 278 0.0055556 0.0033333 0.00138889
63.56 970 410 370 316 0.0055556 0.0033333 0.00138889
77 968 460 420 382 0.0055556 0.0033333 0.00138889
84.5 1269 409 369 326 0.0055556 0.0033333 0.00138889
71.42 961 372 343 323 0.0055556 0.0033333 0.00138889
So what I would like is a column that gives the gradient for each row with the x variables of threetime, fivetime and twelvetime (do they have to be part of the table anyway?) and the y variables of threemin fivemin and twelvemin.
Then another column with the y-intercept.
I have tried the following to no scuess
T.CP = polyfit((threetime, fivetime, twelvetime),(threemin, fivemin, twelvemin),1)
T.CP = polyfit([threetime, fivetime, twelvetime],[threemin, fivemin, twelvemin],1)

  0 Comments

Sign in to comment.

Answers (1)

Peter Perkins
Peter Perkins on 5 May 2020
"do they have to be part of the table anyway?"
polyfit doesn't accept tables, so yes and no. You will likely find the management of those eight numeric vectors, plus one more, easier when they are wrapped up in a table. But you will need to pass polyfit numeric matrices. What
X = [T.threetime T.fivetime T.twelvetime];
or
X = T{:,["threetime" "fivetime" "twelvetime"]}
or even
X = T{:,6:8}
and similarly for Y, then
T.CP = polyfit(X,Y,1)
But actually, I'm not sure passing a matrix to polyfit would do what you intend anyway. From the polyfit doc: " If x is not a vector, then polyfit converts it into a column vector x(:)". I leave that up to you to figure out, because I don't know what you intend.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by