Borrar filtros
Borrar filtros

Indexing between two lists

6 visualizaciones (últimos 30 días)
bene1
bene1 el 6 de Dic. de 2020
Comentada: bene1 el 6 de Dic. de 2020
Hello. I am fairly new to Matlab and looking for some help with the logic of indexing between two lists.
Matrix A has known distances between points.
A =
% From To DistHzPub
1 2 100
1 3 400
1 4 1000
2 3 300
2 4 900
3 4 600
Matrix B has multiple measurements between the same points.
B =
% From To DistHorz
1 2 100.01
1 2 100.04
1 3 400.02
1 3 400.05
1 4 1000.03
1 4 1000.01
2 3 300.00
2 3 300.04
2 4 900.06
2 4 900.02
3 4 600.03
3 4 600.04
My goal is to match the known distances with the measurements, like this.
C =
% From To DistHzPub
1 2 100
1 2 100
1 3 400
1 3 400
1 4 1000
1 4 1000
2 3 300
2 3 300
2 4 900
2 4 900
3 4 600
3 4 600
Creating an index by hand will get me there...
idx = [1 1 2 2 3 3 4 4 5 5 6 6]'
C = A(idx,:)
... but I'm having a hard time figuring out how to generate the index dynamically. Matrix A and B are subject to change in values, size, and order. Thank you.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 6 de Dic. de 2020
Editada: Ameer Hamza el 6 de Dic. de 2020
It seems that you can just use round() on the 3rd column, but since that solution is so obvious, there might be some other reason that you want another solution.
[~, idx] = ismember(B(:,1:2), A(:,1:2), 'rows');
C = A(idx, :);
  1 comentario
bene1
bene1 el 6 de Dic. de 2020
Perfect. Thank you!
(I simplified the values to understand the process. Rounding won't work with the real data).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by