Dear Numpy Users,
I want to fit a 3d plane into a 3d point cloud and I saw that one could use svd for this purpose. So as I am very fond of numpy I saw that svd was implementented in the linalg module.
Currently I have a numpy array called xyz with n lines (number of points) and 3 columns (x,y,z). I calculated the centroid as : xyz0=npy.mean(xyz, axis=0) #calculate the centroid Next I shift the centroid of the point cloud to the origin with. M=xyzxyz0
next I saw by matlab analogy (http://www.mathworks.co.jp/matlabcentral/newsreader/view_thread/262996) that I can write this :
u,s,vh=numpy.linalg.linalg.svd(M)
Then in the matlab analog they use the last column of vh to get the a,b,c coefficients for the equation a,b,c=vh[:, 1] in numpy
The problem is that the equation ax+by+cz=0 does not represent the plan through my point cloud at all. What am I doing wrong, how can I get the a,b and c coefficients?
Thanks in advance.
Hi Peter
On 5 May 2010 10:02, Peter Schmidtke pschmidtke@mmb.pcb.ub.es wrote:
u,s,vh=numpy.linalg.linalg.svd(M)
Then in the matlab analog they use the last column of vh to get the a,b,c coefficients for the equation a,b,c=vh[:, 1] in numpy
Note that vh is the conjugate transpose of v. You are probably interested in the rows of vh (the columns of V in MATLAB parlance).
Regards Stéfan
Hi,
I just confirmed Stefan's answer on one of the examples in http://www.mathworks.co.jp/matlabcentral/newsreader/view_thread/262996
matlab:
A = randn(100,2)*[2 0;3 0;1 2]'; A = A + randn(size(A))/3; [U,S,V] = svd(A); X = V(:,end)
python:
from numpy import * A = random.randn(100,2)*mat([[2,3,1],[0,0,2]]) A = A + random.randn(100,3)/3.0 u,s,vh = linalg.linalg.svd(A) v = vh.conj().transpose() print v[:,1]
It works! Thanks Peter for bringing this up and Stefan for answering!
Huan
participants (3)

Huan Liu

Peter Schmidtke

Stéfan van der Walt