find the closest value

2 次查看(过去 30 天)
Hi, I have two matrices.
A = [01 105 6; 01 203 12; 02 99 6; 02 306 15];
B = [01 0 0; 01 100 25; 01 200 50; 01 300 75; 02 0 0; 02 100 25; 02 200 50; 02 300 75; 02 400 100];
The 1st columns in A and B are the key numbers. For each key number equal in A and B, I would find the index of closest value in the 2nd column of B compared with the 2nd column in A. Then, I would take the value in the 3th column of B by index. I would obtain the following matrix:
C = [01 105 6 25; 01 203 12 50; 02 99 6 25; 02 306 15 75];
Any suggestion?
Tnx, Gianluca

采纳的回答

Azzi Abdelmalek
Azzi Abdelmalek 2016-6-10
编辑:Azzi Abdelmalek 2016-6-10
A = [01 105 6; 01 203 12; 02 99 6; 02 306 15]
B = [01 0 0; 01 100 25; 01 200 50; 01 300 75; 02 0 0; 02 100 25; 02 200 50; 02 300 75; 02 400 100]
C = [01 105 6 25; 01 203 12 50; 02 99 6 25; 02 306 15 75]
out=A;
for k=1:size(A,1)
ii=ismember(B(:,1),A(k,1));
[~,idx]=min(abs(A(k,2)-B(ii,2)));
out(k,4)=B(idx,3);
end
out

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by