Borrar filtros
Borrar filtros

find the closest value

2 visualizaciones (últimos 30 días)
gianluca
gianluca el 10 de Jun. de 2016
Editada: Azzi Abdelmalek el 10 de Jun. de 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

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 10 de Jun. de 2016
Editada: Azzi Abdelmalek el 10 de Jun. de 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

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by