Borrar filtros
Borrar filtros

How to find rows that contain value in another column vector

5 visualizaciones (últimos 30 días)
Zair Ahmedi
Zair Ahmedi el 12 de Abr. de 2018
Respondida: Walter Roberson el 12 de Abr. de 2018
Let say I have matrix X as follows
X =
1 2 3
1 4 5
1 6 7
2 8 9
2 10 11
3 12 13
3 14 15
3 16 17
4 18 19
4 20 21
5 22 23
5 24 25
and column vector Y
Y=
2
4
5
So, I want to create a matrix where based on values in Y that has the same value of Column 1 of X
So,
Z=
2 8 9
2 10 11
4 18 19
4 20 21
5 22 23
5 24 25

Respuestas (2)

KSSV
KSSV el 12 de Abr. de 2018
Read about ismemebr
X = [1 2 3
1 4 5
1 6 7
2 8 9
2 10 11
3 12 13
3 14 15
3 16 17
4 18 19
4 20 21
5 22 23
5 24 25];
Y=[2
4
5] ;
[ia,ib] = ismember(X(:,1),Y) ;
iwant = X(ia,:)

Walter Roberson
Walter Roberson el 12 de Abr. de 2018
Z = X(ismember(X(:,1), Y), :);
provided that the original order from X is acceptable.
That is, suppose that Y = [2; 5; 4], then would you need the Z to be in a different order?
If the X values had not happened to be in order by column 1, then would you still need the output to be in the same order as the Y values, or could they be in whatever order they were in in X ?

Categorías

Más información sobre Variables 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