Filter löschen
Filter löschen

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

77 Ansichten (letzte 30 Tage)
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 Kommentar
Voss
Voss am 17 Dez. 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).

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 17 Dez. 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

Weitere Antworten (1)

Chunru
Chunru am 17 Dez. 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

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by