title: Java SVD with JAMA or else
body: <p>I have a cloud of Points and I need the best fitting Line. I'm using JAMA but I don't know why, something is not working. Probably it's me who doesn't get how it works. I have a Nx3 Matrix (this is what JAMA svd supports) and I get the right Matrix V from the Svd. The vector I need is the right singular vector (row or coloumn ? ) corresponding to the greatest singular value. This vector is supposed to represent the main direction.</p>
  
  <p>Every vector is supposed to have y as the greatest positive value, x can be both positive or negative and z is supposed to be negative. But sometimes the vector I get has a negative y value, or anyway it's pointing in the wrong direction.</p>
  
  <p>My cloud of Points are pretty regular, the are all postioned almost along the y axis (with z small and negative). So the main direction is supposed to be really easy to find. But it's still not working properly.</p>
  
  <p>In this case I'm getting the row vector (I tried the coloumn vector too) of the right Matrix V. I already substracted the centroid from "pointSet".</p>
  
  <pre><code>public static Matrix bestDirection(Matrix pointSet){
  
      Matrix bestFittingLine = new Matrix(3,1);
      SingularValueDecomposition svd = pointSet.svd();
  
      bestFittingLine.set(0, 0, svd.getV().get(0, 0));
      bestFittingLine.set(1, 0, svd.getV().get(0, 1));
      bestFittingLine.set(2, 0, svd.getV().get(0, 2));
  
      return bestFittingLine;
  }
  </code></pre>
  
  <p>I guess maybe I'm not considering something. Idk maybe I should use another technique or another library.</p>