How to get the regression from 2 vectors

8 views (last 30 days)
Shani
Shani on 22 Nov 2013
Edited: Image Analyst on 23 Nov 2013
Hi, I have 2 vectors and I would like to know the R^2 ie the regression of the line. I would like a line of best fit to show on the plot and the two vectors one on y and one on x axis thanks
  1 Comment
Image Analyst
Image Analyst on 23 Nov 2013
Edited: Image Analyst on 23 Nov 2013
Here is what Shani asked, so that it will still be here when she deletes this question:
subject line: How to get the regression from 2 vectors
Hi, I have 2 vectors and I would like to know the R^2 ie the regression of the line. I would like a line of best fit to show on the plot and the two vectors one on y and one on x axis thanks

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 22 Nov 2013
Try this demo:
% Create sample data and plot it.
x = 1:10;
y = 3*x - 20 + 4*rand(1, length(x)); % Some equation
plot(x, y, 'r*', 'MarkerSize', 15);
grid on;
% Get the fit
coeffs = polyfit(x, y, 1);
% Get the x values for the fit at higher resolution.
xFit = linspace(x(1), x(end), 300);
% Get the estimated y values
yFit = polyval(coeffs, xFit);
% Plot them as a line.
hold on;
plot(xFit, yFit, 'b-', 'LineWidth', 3);
  1 Comment
Image Analyst
Image Analyst on 22 Nov 2013
Edited: Image Analyst on 22 Nov 2013
My x and y are vectors. What do you have? Give code to generate the kind of data you have.

Sign in to comment.

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!