필터 지우기
필터 지우기

find the closest value

조회 수: 2 (최근 30일)
gianluca
gianluca 2016년 6월 10일
편집: Azzi Abdelmalek 2016년 6월 10일
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