Filter löschen
Filter löschen

find the closest value

2 Ansichten (letzte 30 Tage)
gianluca
gianluca am 10 Jun. 2016
Bearbeitet: Azzi Abdelmalek am 10 Jun. 2016
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

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 10 Jun. 2016
Bearbeitet: Azzi Abdelmalek am 10 Jun. 2016
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

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by