简体   繁体   English

获取/比较matlab / octave数组的差异

[英]Getting/comparing difference in matlab/octave arrays

I have two arrays A and B and I would like to find the values in the first column in A that are closest to the values in the first column in B and sort them by difference/closeness. 我有两个数组A和B,我想在A的第一列中找到最接近B的第一列中的值的值,并按差异/接近度对它们进行排序。

Example: A is 29x2 and B is 14x2 示例:A为29x2,B为14x2

A=[0, 2;
408, 642;
492, 66;
1527, 108;
1560, 16;
1755, 182;
1809, 10;
2133, 42;
2235, 444;
2289, 4191;
2334, 86;
2661, 22;
2709, 2;
2787, 2652;
3072, 200;
3081, 110;
3147, 638;
3963, 10;
4080, 2;
4332, 1674;
4462, 14;
5148, 1042;
5649, 8;
5895, 44;
6078, 284;
6315, 2;
6989, 4;
7485, 46;
7623, 3458;
];


B= [0, 2;
165, 2114;
572, 122;
576, 5416;
581, 176;
583, 278;
653, 28;
655, 4;
656, 154;
657, 16;
658, 4188;
665, 3475;
903, 20;
1145, 8;
]

The final arrays needs to be sorted by the closest/nearest x values (1st column) in it. 最终数组需要按其中最接近/最接近的x值(第1列)进行排序。 so when the C array is generated it would have [0,0;492,572] etc... 所以当生成C数组时,它会有[0,0; ​​492,572]等...

I included two plots to give a graphic example. 我包括两个图来给出一个图形示例。 The first plot shows all the data points of A and B represented as stars and circles and the second plot is zoomed in to show the nearest points between the arrays the final array would have the items that are circled in orange. 第一个图显示A和B的所有数据点,表示为星形和圆形,第​​二个图放大以显示数组之间的最近点,最终数组将具有以橙色圈出的项目。 (Please ignore the y axis in the plot I'm just interested in how close the values are on the x axis) (请忽略图中的y轴我只对x轴上的值有多接近感兴趣)

How can I create the C array using matlab/octave? 如何使用matlab / octave创建C数组

图1完整情节

图2放大了情节

bsxfun approach - bsxfun方法 -

%// Find the minimum absolute diferences and corresponding indices for each
%// element in the first column of B against all other elements in the 
%// first column of A.
[min_val,min_ind] = min(abs(bsxfun(@minus,A(:,1),B(:,1)'))) %//'

%// Find the sorted indices for the minimum absolute diferences
[~,sorted_ind] = sort(min_val)

%// Index into A using min_ind to assciate each element in the first
%// column of B against the corresponding closest element in the first
%// column of A.
C = [A(min_ind,1) B(:,1)]

%// Sort rows of C acoording to sorted_indices to have the desired output
C = C(sorted_ind,:)

Alternative approach using pdist2 [ Pairwise distance between two sets of observations ], would require just one change in the above code - 使用pdist2替代方法[ 两组观察之间的成对距离 ],只需要在上面的代码中进行一次更改 -

[min_val,min_ind] = min(pdist2(A(:,1),B(:,1)))

To find the nearest point in A corresponding to each value in B, use interp1 with the nearest mode. 要在A中找到与B中每个值对应的最近点,请使用interp1nearest模式。

AnearB = interp1(A(:,1), A(:,1), B(:,1), 'nearest');
C = [AnearB B(:,1)];

Then sort the results by closeness. 然后按接近度对结果进行排序。

dist = abs(diff(C'));
[~, order] = sort(dist);
C = C(order,:);

Results for your sample data 您的样本数据的结果

 C = 0 0 492 572 492 576 492 581 492 583 492 653 492 655 492 656 0 165 492 657 492 658 492 665 1527 1145 492 903 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM