필터 지우기
필터 지우기

How to delete row from matrix, which has values 0 and 4?

조회 수: 1 (최근 30일)
Beibit Sautbek
Beibit Sautbek 2016년 7월 21일
편집: Stephen23 2016년 7월 21일
I have a matrix:
u =
0 0 4
0 3 0
0 3 4
2 0 0
2 0 4
2 3 0
2 3 4
I need to delete a row which has value 4 and other values equal to 0.
So, my result should delete the first row, where [0 0 4]. How Can I do?
I did this code below, but it doesn't work.
d=length(u);
if u(1:d,:)==0 & u(1:d,:)==4
u(1:d,:) = []
end
Could anyone help me?

채택된 답변

Stephen23
Stephen23 2016년 7월 21일
편집: Stephen23 2016년 7월 21일
u = [...
0 0 4
0 3 0
0 3 4
2 0 0
2 0 4
2 3 0
2 3 4]
idx = all(u==0 | u==4, 2)
out = u(~idx,:)
  댓글 수: 1
Stephen23
Stephen23 2016년 7월 21일
편집: Stephen23 2016년 7월 21일
Addendum: if you need to check that in any row exactly one element has the value four, and all remaining elements have the value zero, then this will work:
idx = size(u,2)-1==sum(u==0,2) & 1==sum(u==4,2)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by