Mapping 2 vectors - help to vectorize

前端 未结 6 1154
情话喂你
情话喂你 2020-11-30 12:34

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         


        
6条回答
  •  一整个雨季
    2020-11-30 13:30

    Oh! One other option: since you're looking for close correspondences between two sorted lists, you could go through them both simultaneously, using a merge-like algorithm. This should be O(max(length(xm), length(xn)))-ish.

    
    match_for_xn = zeros(length(xn), 1);
    last_M = 1;
    for N = 1:length(xn)
      % search through M until we find a match.
      for M = last_M:length(xm)
        dist_to_curr = abs(xm(M) - xn(N));
        dist_to_next = abs(xm(M+1) - xn(N));
    
        if dist_to_next > dist_to_curr
          match_for_xn(N) = M;
          last_M = M;
          break
        else
          continue
        end
    
      end % M
    end % N
    

    EDIT: See @yuk's comment, the above code is not totally correct!

提交回复
热议问题