# testing collinearity with algorithm collinear7

조회 수: 11(최근 30일)
Jürgen 2012년 12월 8일
Hi,
I was looking for an algorithm to test collinearity and found : http://blogs.mathworks.com/loren/2008/06/06/collinearity/
function tf = collinear7(p1,p2,p3)
mat = [p1(1)-p3(1) p1(2)-p3(2); ...
p2(1)-p3(1) p2(2)-p3(2)];
tf = det(mat) == 0;
works fine if you test it with eg: p1 = [1 1]; p2 = [3.5 3.5]; p3 = [-7.2 -7.2]; collinear(p1,p2,p3) ans=1
for testing I choose: p1 = [ 123.83 -205.59] p2 = [ 138.38 -990.38] p3=(p1+p2)/2 p3 = 131.11 -597.98
collinear(p1,p2,p3) ans = 0 here I was suprised because two points and the middle point should be collinear no? so I tried it again with p1=[1.5 -5.3] p2=[ 5 -3.8] p3=(p1+p2)/2 collinear(p1,p2,p3) ans = 1 so now it is ok again???
so I tested another approach slightky changed the code from the site
function tf = collinear1(p1,p2,p3)
m = slope(p1,p2);
b = intercept(p1,p2);
y = (m*p3(1)+b) ;
tf=abs(y- p3(2))<0.000001;
end
function m = slope(p1,p2)
m = (p2(2)-p1(2))/(p2(1)-p1(1));
end
function b = intercept(p1,p2)
m = slope(p1,p2);
b = p1(2)-m*p1(1);
end
with p1 = [ 123.83 -205.59] p2 = [ 138.38 -990.38] p3=(p1+p2)/2
collinear1(p1,p2,p3) is 1=> so collinear?
I can only think of numerical errors to explain this or am I doing something completely wrong?
regards,J

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

### 채택된 답변

Matt J 2012년 12월 8일
편집: Matt J 2012년 12월 12일
Yes, it's a numerical problem. The first version collinear7 uses the criterion
det(mat) == 0
which demands both perfect collinearity among the points and a perfect determinant calculation, with no tolerance for floating point errors. The other versions apply a tolerance on floating point inaccuracies.

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

### 범주

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by