How to fit the best line

1 view (last 30 days)
Guy
Guy on 27 Sep 2012
Hello,
I have a problem trying to choose out of two vectors the one vector that is most similar to a sample vector. My function receives a couple of vectors and a sample vector every time. It then needs to compare each one of them to the sample, and choose the best fit. The vectors represent lines and the values in each vector represent the line's dots, so actually I need to fit the best line pattern to the sample.
My problem is that sometimes I need to fit a vector of zeros to a vector of very small values. So, for example if the two vectors are: a = [ 0 0 0 ] ; b= [2 1 1] and the sample is c = [ 0.001 0.001 0.001], I would like to get 2 coefficients, according to which I will be able to decide that the 'a' vector is the better fit. The basket case is, I cannot use xcorr(a,c,0,'coeff') function or corrcoef, since a vector of zeros will not give me a correlation coefficient, but will present NaN result instead.
I will also mention that the two vectors and the sample are the outcome of robustfit function, so actually I can use the lines' coefficients rather than the actual point vectors and call them through the function, if you guys think it's easier/better.
What do I do?

Accepted Answer

Tom Lane
Tom Lane on 27 Sep 2012
Would it be reasonable to compare norm(a-c) with norm(b-c)?
  3 Comments
Guy
Guy on 28 Sep 2012
Norm gives me an indication of the relative length of a vector doesn't it? Nothing to coclude about its direction, which is probably the most important for me to take into consideration while trying to correlate the vectors
Tom Lane
Tom Lane on 29 Sep 2012
There is a pdist2 function in the Statistics Toolbox and you can use it to compute the cosine distance. However, as you point out, the cosine to the zero vector is undefined. I'm not sure how you would quantify your notion that the zero vector is "closer" to a vector with small elements, but other vectors have a distance related to the angle between them.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!