Working in Matlab I have 2 vectors of x coordinate with different length. For example:
xm = [15 20 24 25 26 35 81 84 93];
xn = [14 22 26 51 55 59 70 75 89 96];
I need to map xm to xn, or in other words to find which coordinates in xn are closest to xm. So if I have values associated with those coordinates, I can use this map as index and correlate those values.
Both vectors are sorted and there are no duplicates in each vector.
I wrote a simple function with for-loop:
function xmap = vectors_map(xm,xn)
xmap = zeros(size(xm));
for k=1:numel(xm)
[~, ind] = min(abs(xm(k)-xn));
xmap(k) = ind(1);
end
For the above example is returns
xmap =
1 2 2 3 3 3 8 9 10
It works ok, but takes a while with long vectors (over 100,000 points).
Any ideas how to vectorize this code?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…