Fitting of two vectors
Show older comments
Hi,
I have two vectors x1, x2 which have similar values but are offset in both axis. I would like to move x2 by keeping the vector x1 fixed such that x2 matches (overlaps/fits) x1. I have also attached an image for reference
Many thanks for the answers
2 Comments
per isakson
on 5 Oct 2017
Homework?
John D'Errico
on 5 Oct 2017
I doubt this is homework, because it has been expressed in a very ambiguous way. Homework assignments would (should) always be written so the student knows what they are supposed to do.
Answers (1)
John D'Errico
on 5 Oct 2017
Assuming this is not homework, there is a huge question one must ask. Mainly, what is the model?
Ok, first, you don't have two vectors, x1 and x2. Since you have drawn them on (x,y) axes, AND you say they vary in both x and y, you MUST have then as curves drawn from ordered pairs, thus x1 is really a set of ordered pairs (x1,y1) that form a curve in the plane. Likewise, x2 is really x2,y2.
So I assume that you MUST have FOUR vectors. This is not a question. Merely a statement of fact, based on what you have asked.
Now, you talk about an offset in both x and y. But is that all? Could the second curve have been rotated from the first? Could it have been stretched, so that it is longer or shorter than the first?
The point is, a simple translational offset is trivial, as are the other relationships. But you need to ask clearly what you need, as otherwise there is no answer to the question. So, for a simple offset in x and y, we would compute the least squares estimates of the offset as (given vectors x1,y1,x2,y2)
xoffset = mean(x2 - x1);
yoffset = mean(y2 - y1);
That is all that you need to estimate a model such that
x2 = x1 + xoffset
y2 = y1 + yoffset
So xoffset is the translation in x, yoffset is the translation in y.
However, if you wanted a rotation, or something more sophisticated, then one would need to create an actual linear regression model. Still, not more than roughly one line of code.
2 Comments
Rik
on 5 Oct 2017
I was just about to write something similar, but this time the page refreshed just in time to see that it would have been a double post.
John D'Errico
on 5 Oct 2017
... Which happens all the time. You might have had something useful to add, or said it in a way more clear than did I. So it never hurts, except as a less valuable use of your time. :)
Categories
Find more on Multidimensional Arrays 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!