Clear Filters
Clear Filters

Draw a line throw a set of point

6 views (last 30 days)
Hi i have a set of poin in an immage and i want to draw a line that is madiated by the positition of the points. I want something like this:
  1 Comment
Sean Doherty
Sean Doherty on 3 Nov 2020
you could use polyfit()
this example is from polyfit help:
% Fit a polynomial p of degree 1 to the (x,y) data:
x = 1:50;
y = -0.3*x + 2*randn(1,50);
p = polyfit(x,y,1);
% Evaluate the fitted polynomial p and plot:
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
legend('data','linear fit')

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 3 Nov 2020
Looks like he has a solution in his duplicate question: Click here for it

More Answers (1)

Rik
Rik on 3 Nov 2020
For this code to work, you will need the attached fit_line function, and my point_to_line_distance, which you can find on the FEX or through the add-on manager.
x=[0.9, 3.2, 0.2, -0.0, -10.0, -1.2, 2.5, 2.9, 15.5, 4.3, -3.5, -3.6, -1.7, 0.5, -1.5, 12.0, ...
4.6, 6.4, -0.5, -1.1, -4.7, -0.2, -1.5, -3.5, -3.6, -0.1, -2.6, -11.5, 0.3, -0.2, 0.1, ...
-3.7, -0.1, 3.9, 11.4, -11.3, 1.4, -2.1, 8.7, -7.4, 1.4, -6.1, 2.6, 0.3, 1.2, 4.9, -1.0];
y=[5.0, -1.8, 2.3, 4.8, 1.4, 3.2, 3.7, 5.3, 4.3, 5.2, 2.6, 1.8, -4.3, 3.0, -0.5, 3.2, 5.0, ...
0.3, 6.2, -4.7, -3.3, 0.8, -0.4, 1.3, -6.0, 4.1, 1.6, 1.1, 3.8, -1.6, 1.1, 2.9, -0.7, ...
0.4, 10.7, -3.0, -0.8, 4.3, 3.3, -0.8, 5.8, 2.9, 1.9, 1.4, 6.2, 5.2, 1.6];
plot(x,y,'+r')
x_bounds=xlim;
p=fit_line(x,y);
hold on,plot(x_bounds,polyval(p ,x_bounds),'r');hold off
  2 Comments
valerio auricchio
valerio auricchio on 3 Nov 2020
What are the point_to_line? And how can i import the function?
Rik
Rik on 3 Nov 2020
Have you clicked the link? That would probably help...

Sign in to comment.

Categories

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