필터 지우기
필터 지우기

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
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
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,:)
new = 2×6
0 50 51 52 50 1 0 51 51 54 50 1

추가 답변 (1개)

Chunru
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]
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

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by