Effacer les filtres
Effacer les filtres

find the closest value

2 vues (au cours des 30 derniers jours)
gianluca
gianluca le 10 Juin 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

Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 10 Juin 2016
Modifié(e) : Azzi Abdelmalek le 10 Juin 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

Plus de réponses (0)

Catégories

En savoir plus sur Matrix Indexing dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by