How can I find all the rows that have a specific value in a specific column of an array?
77 次查看(过去 30 天)
显示 更早的评论
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 个评论
Voss
2021-12-17
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).
采纳的回答
Voss
2021-12-17
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 个评论
更多回答(1 个)
Chunru
2021-12-17
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrices and Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!