Effacer les filtres
Effacer les filtres

How can I find all the rows that have a specific value in a specific column of an array?

77 vues (au cours des 30 derniers jours)
I have an array with 6 columns, four of which can have identical values. I want to find all the rows that have a certain value in column 5 and put them into a new matrix, but avoiding rows that have the value in a different place.
Example:
If I have
ex=[0 50 51 52 50 1;0 53 50 52 54 1;0 51 51 54 50 1;0 53 51 52 53 1;0 50 54 52 51 1]
I want to find the rows that have 50 in column 5 (1 and 3, in this case, but not 2 or 5, since they have 50 in the wrong position) and put them into a new matrix so that
new=[0 50 51 52 50 1;0 51 51 54 50 1].
  1 commentaire
Voss
Voss le 17 Déc 2021
If I understand correctly, you do not need to specify that rows containing the certain value in the wrong column need to be avoided, because rows that do not contain the certain value are also avoided. I mean, these two 'types' of rows are treated the same, so the only distinction necessary is whether the row has a certain value in column 5 or not.
In the example, rows 2, 4 and 5 are all avoided (because they do not have the value 50 in column 5), while rows 1 and 3 are selected (because they do have a 50 in column 5).

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 17 Déc 2021
ex = [0 50 51 52 50 1;0 53 50 52 54 1;0 51 51 54 50 1;0 53 51 52 53 1;0 50 54 52 51 1];
new = ex(ex(:,5) == 50,:)
new = 2×6
0 50 51 52 50 1 0 51 51 54 50 1

Plus de réponses (1)

Chunru
Chunru le 17 Déc 2021
ex=[0 50 51 52 50 1;0 53 50 52 54 1;0 51 51 54 50 1;0 53 51 52 53 1;0 50 54 52 51 1]
ex = 5×6
0 50 51 52 50 1 0 53 50 52 54 1 0 51 51 54 50 1 0 53 51 52 53 1 0 50 54 52 51 1
I want to find the rows that have 50 in column 5 (1 and 3, in this case, but not 2 or 5, since they have 50 in the wrong position) and put them into a new matrix so that
new=[0 50 51 52 50 1;0 51 51 54 50 1]
new = 2×6
0 50 51 52 50 1 0 51 51 54 50 1
x = ex(ex(:, 5)==50, :)
x = 2×6
0 50 51 52 50 1 0 51 51 54 50 1

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by