How can I find all the rows that have a specific value in a specific column of an array?
77 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
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).
Akzeptierte Antwort
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,:)
0 Kommentare
Weitere Antworten (1)
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]
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]
x = ex(ex(:, 5)==50, :)
0 Kommentare
Siehe auch
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!