Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Determine if points are in a straight line?

Subject: Determine if points are in a straight line?

From: J D

Date: 10 Sep, 2010 19:04:23

Message: 1 of 10

Is there a quick way to check if a set of points are in a straight line?

Ex/
 data = [8.6401 4.2324
            8.6465 4.2592
            8.6453 4.2542]

islinear(data(:,1),data(:,2)) = 1

Thanks

Subject: Determine if points are in a straight line?

From: Matt Fig

Date: 10 Sep, 2010 19:20:20

Message: 2 of 10

Are these supposed to be coordinate pairs? If so you could use the first two points to define one slope and the second two to define a second slope. if the two slopes are the same, they are on a line. For example:

f = @(x) 3*x + 9; % A line
data = [2 f(2);5 f(5); 11 f(11)] % Some data points
D = diff(data);
Slopes = D(:,2)./D(:,1)

Subject: Determine if points are in a straight line?

From: Walter Roberson

Date: 10 Sep, 2010 19:28:15

Message: 3 of 10

On 10-09-10 02:04 PM, J D wrote:
> Is there a quick way to check if a set of points are in a straight line?
>
> Ex/
> data = [8.6401 4.2324
> 8.6465 4.2592
> 8.6453 4.2542]
>
> islinear(data(:,1),data(:,2)) = 1

 >> Q = polyfit(data(:,1),data(:,2),1);
Q =
           4.18894009216628 -31.9604585253489
 >> Q(1) * data(:,1) + Q(2) - data(:,2)
ans =
       2.76497695672617e-06
       1.19815668178092e-05
      -1.47465437754235e-05

As these values are not 0 to within a small multiple of eps(data(:,1)), it
follows that the points are NOT in a straight line.

 >> Q = polyfit(data(:,1),data(:,2),2)
Q =
          -4.00641025800001 73.4447115659304 -331.253406088208
 >> Q(1) * data(:,1).^2 + Q(2) * data(:,1) + Q(3) - data(:,2)
ans =
       4.08562073062058e-14
       -9.2370555648813e-14
      -8.79296635503124e-14

Thus with a quadratic, the fit is good and by examining the coefficients the
points are clearly non-linear.

Subject: Determine if points are in a straight line?

From: Sean

Date: 10 Sep, 2010 19:33:06

Message: 4 of 10

"J D" <JD@USA.com> wrote in message <i6dvfn$qg3$1@fred.mathworks.com>...
> Is there a quick way to check if a set of points are in a straight line?
>
> Ex/
> data = [8.6401 4.2324
> 8.6465 4.2592
> 8.6453 4.2542]
>
> islinear(data(:,1),data(:,2)) = 1
>
> Thanks

One way:
%%
%A is your data
A = [8.6401 4.2324
8.6465 4.2592
8.6453 4.2542];

%calculate slope and intercept for best fit line
mb = [A(:,1), ones(3,1)]\A(:,2);

%Function handle to see if each residual from the points to best fit line is less than some tolerance:
islinear = @(mb,A,tol)all(abs((A(:,1).*mb(1)+mb(2)) - A(:,2))<tol);

%run it.
islinear(mb,A,.01)

%SCd

Subject: Determine if points are in a straight line?

From: Roger Stafford

Date: 10 Sep, 2010 19:54:23

Message: 5 of 10

"J D" <JD@USA.com> wrote in message <i6dvfn$qg3$1@fred.mathworks.com>...
> Is there a quick way to check if a set of points are in a straight line?
>
> Ex/
> data = [8.6401 4.2324
> 8.6465 4.2592
> 8.6453 4.2542]
>
> islinear(data(:,1),data(:,2)) = 1
>
> Thanks
- - - - - - - - - -
  Another way:

 abs(det([data,ones(3,1)])) < tol

for suitably small value of tol.

Roger Stafford

Subject: Determine if points are in a straight line?

From: Bruno Luong

Date: 10 Sep, 2010 22:26:05

Message: 6 of 10

"J D" <JD@USA.com> wrote in message <i6dvfn$qg3$1@fred.mathworks.com>...
> Is there a quick way to check if a set of points are in a straight line?
>
> Ex/
> data = [8.6401 4.2324
> 8.6465 4.2592
> 8.6453 4.2542]
>
> islinear(data(:,1),data(:,2)) = 1
>
> Thanks

another way is to check the vectors are colinear after shift them:

tol = 1e-4 % <- select appropriate small tolerance
rank(bsxfun(@minus,data,data(1,:)),tol)==1

Bruno

Subject: Determine if points are in a straight line?

From: Jan Simon

Date: 12 Sep, 2010 22:50:28

Message: 7 of 10

Dear Walter,

> > Is there a quick way to check if a set of points are in a straight line?

> As these values are not 0 to within a small multiple of eps(data(:,1)), it
> follows that the points are NOT in a straight line.
>
> Thus with a quadratic, the fit is good and by examining the coefficients the
> points are clearly non-linear.

There can be a problem if the number of points exceed 3:
  polyfit(1:1e5, sin(1:1e5), 2)
  >> 5.5e-14, -5.5e-9, 0.00011
So the curvature is small, but the points are not on a straight line.
I admit, the example is pathological. But a *general* approach to solve the problem is not trivial.

Kind regards, Jan

Subject: Determine if points are in a straight line?

From: Roger Stafford

Date: 12 Sep, 2010 23:34:05

Message: 8 of 10

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i6jlfk$klf$1@fred.mathworks.com>...
> There can be a problem if the number of points exceed 3:
> polyfit(1:1e5, sin(1:1e5), 2)
> >> 5.5e-14, -5.5e-9, 0.00011
> So the curvature is small, but the points are not on a straight line.
> I admit, the example is pathological. But a *general* approach to solve the problem is not trivial.
>
> Kind regards, Jan
- - - - - - - -
  Bruno's formula in terms of rank is valid in general for n points using an n by 2 array of points' coordinates for 'data' and properly selected tolerance (though I would prefer subtracting the mean of the points rather than the end point.)

Roger Stafford

Subject: Determine if points are in a straight line?

From: Loren Shure

Date: 13 Sep, 2010 12:23:36

Message: 9 of 10


"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in
message news:i6e2df$a8c$1@fred.mathworks.com...
> "J D" <JD@USA.com> wrote in message <i6dvfn$qg3$1@fred.mathworks.com>...
>> Is there a quick way to check if a set of points are in a straight line?
>>
>> Ex/
>> data = [8.6401 4.2324
>> 8.6465 4.2592
>> 8.6453 4.2542]
>>
>> islinear(data(:,1),data(:,2)) = 1
>>
>> Thanks
> - - - - - - - - - -
> Another way:
>
> abs(det([data,ones(3,1)])) < tol
>
> for suitably small value of tol.
>
> Roger Stafford
>

See this post and all of the comments with it:

http://blogs.mathworks.com/loren/2008/06/06/collinearity/

--
Loren
http://blogs.mathworks.com/loren/
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Determine if points are in a straight line?

From: Jan Simon

Date: 14 Sep, 2010 17:55:23

Message: 10 of 10

Dear Loren,

> See this post and all of the comments with it:
> http://blogs.mathworks.com/loren/2008/06/06/collinearity/

Very usefiul! Thanks, Jan

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us