How to remove some certain rows in Matlab?

조회 수: 2 (최근 30일)
Moe
Moe 2016년 2월 22일
답변: Stephen23 2016년 2월 22일
Matrix A is as follows:
A = [45,1,1,1,2;74,1,2,3,1;75,1,1,3,1;73,1,1,1,1;53,1,1,3,2;98,1,1,1,1];
I want to find those rows in matrix A that only has array "1" and remove them from matrix A (produce new matrix C without those rows) and store them in the matrix B.
B = [45,1,1,1,2;74,1,2,3,1;75,1,1,3,1;53,1,1,3,2];
C = [73,1,1,1,1;98,1,1,1,1]
  댓글 수: 2
jgg
jgg 2016년 2월 22일
Do you mean columns? Because no row in A has only ones?
Moe
Moe 2016년 2월 22일
jgg, column 2 to column 5. Remove the row if there is only array "1" in for all specified columns in on a row.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 2월 22일
Simple logical operations are all that is required:
>> idx = all(1==A(:,2:end),2);
>> B = A(~idx,:)
B =
45 1 1 1 2
74 1 2 3 1
75 1 1 3 1
53 1 1 3 2
>> C = A(idx,:)
C =
73 1 1 1 1
98 1 1 1 1

추가 답변 (1개)

jgg
jgg 2016년 2월 22일
편집: jgg 2016년 2월 22일
Ah, I think I understand: the first column are IDs or something. This should work, using the ismember function with the rows option:
data = A(:,2:end);
ind = ismember(data,ones(1,size(data,2)),'rows');
C = A(ind,:);
B = A(~ind,:);

카테고리

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