How to plot a single column of values with a linear regression line through it

조회 수: 2 (최근 30일)
I have a column that I wann plot and have a regression line go through it, How would I do that?
my code so far is:
figure(1)
title('Phase1')
plot(FG)
  댓글 수: 5
darova
darova 2020년 2월 24일
  • I added the: save('answers.mat', 'X', 'Y'); to both codes and still didnt the regression line.
Image Analyst can you cooperate?
isamh
isamh 2020년 2월 24일
편집: isamh 2020년 2월 24일
Lol I just noticed what was wrong with that. Thought he wanted me to do something else, I’m unable to provide the data now and was wondering if we could just use random data. Forgot to mention that i have some NAN rows with both columns, would that effect the results?

댓글을 달려면 로그인하십시오.

채택된 답변

darova
darova 2020년 2월 24일
Here is a simple example (i made it special for you)
N = 20;
x = linspace(0,3,N);
y = 2*x + rand(1,N);
p = polyfit(x,y,1);
y1 = polyval(p,x);
plot(x,y,'.r')
hold on
plot(x,y1,'b')
hold off
  • Forgot to mention that i have some NAN rows with both columns, would that effect the results?
Then you should do interpolation without NaN
ix = ~isnan(y); % choose indices that are not NaN
x1 = x(ix);
y1 = y(ix);
p = polyfit(x1,y1,1);
y2 = polyval(p,x1);
  댓글 수: 2
isamh
isamh 2020년 2월 24일
Trued that but I believe the columns are now unequal lengths. I get this error message: error with p1=polyfit(X1,Y1,1);
darova
darova 2020년 2월 24일
Please post the entire error and code you used

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by