2

I am very sorry if similar question was asked before. I have two vectors of 50 x n, and want to find the shift of the first vector, which provides the closest match with the second vector. I have tried fminsearch and circshift in Matlab, however, could not exactly find how to use them in this case.

I also saw the discussion here, however, I do not need a representative function or interpolation to match the two vectors. Instead, I just want the best shift of one profile, which minimizes the differences along the profile. I will be happy if you can direct me. Also, could I use fminsearch to solve this problem?

Community
  • 1
  • 1
Tethus
  • 23
  • 2

2 Answers2

3

You can do it as follows with gallery's circulant matrix:

%// example data
A = randi([1,10],[1,10])
B = circshift(A,[0,3])

With this data the matching shift is expected to be 3

[~,minidx] = min(sum(abs(bsxfun(@minus,A,gallery('circul',B))),2))
shift = numel(A) - minidx + 1

shift =

     3

Explanation

%// circulant matrix 
circul = gallery('circul',B)
%// substract vector A from all shifted rows of circulant matrix
diffs = bsxfun(@minus,A,circul)
%// sum absolute differences
C = sum(abs(diffs),2)
%// find index of row with minimum difference
[~,minidx] = min(C)
%// depending on your defintion of "shift", means
%// depending on where you start to count, you may want to
%// change this:
shift = numel(A) - minidx + 1

Things to consider

This is a kind of brute-force method, vectorized and efficient though. But it analyzes all possible shifts which could occur to vector B. If you have really long vectors (numel > 10000+) you could run into memory problems where a fminsearch wouldn't. On the other hand you can never be sure fminsearch would find the best solution, unless your data follows a clear pattern or you analyze all shifts anyway, but then it's gonna be slow as well.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
  • Thank you very much for very well explanation! That's a very efficient way and worked quite well! I have 2 temperature profiles (each having dimensions of maximum 300 x 1), and have the exact vertical coordinates for the second profile, however, the vertical coordinates of the first profile is not clear. I would like to find these vertical coordinates (in sub-mm scale), which best match these two profiles (e.g. with an upper bound). Which function would work for this case? I thought of finding two equations (as suggested below) which match two profiles, but I am not sure to where I can reach. – Tethus Sep 15 '15 at 08:36
  • @Tethus So you mean the "real" shift is not an integer? Then I would take the rows returned by the above function and the ones below and above, interpolate and repeat the procedure. – Robert Seifert Sep 15 '15 at 09:29
  • @ thewaywewalk that worked well. Thanks again for your input and the idea! – Tethus Sep 22 '15 at 07:08
0

Isn't this just OLS? To clarify, I assume the second matrix is Y and the first matrix is X, both have dimension 50 x n for some pre-specified value for n. For simplicity, stack the matrix, so that X and Y are (50 x n) x 1 (50xn rows and 1 column. Then your model is

Y_i = a + bX_i + e_i.

Your goal is to minimize the sum of squared errors:

min_{a,b} sum_i e_i^2. 

You can either use fmincon or solve for the analytical solution. See simple linear regression for formulas:

https://en.wikipedia.org/wiki/Simple_linear_regression