Mapping 2 vectors - help to vectorize

前端 未结 6 1165
情话喂你
情话喂你 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:39

    Taking advantage of being sorted, as David says, will be faster since you have so many points, but for reference one way to vectorize this would be to use meshgrid:

    [X Y] = meshgrid(xn, xm);
    diffs = X - y;
    mins = min(diffs, [], 2);
    

    Note that this will create two 100,000 x 100,000 arrays in memory, so it's probably only feasible for smaller data sets.

提交回复
热议问题