필터 지우기
필터 지우기

how to delete a row from a matrix

조회 수: 3 (최근 30일)
nadia nadi
nadia nadi 2015년 10월 25일
편집: Stephen23 2015년 10월 25일
Dear,
I need to delete rows with all zeros from B and corresponding ones from b, where Bx>=b, so I wrote this code but I got error because of the index, can anyone check it for me please and if there is any faster way will be very helpful. Thanks in advance.
Nadia
B=[
1 0 0 0 0 0 0
0 0 0 1 0 0 0
0 0 0 0 0 0 0
0 1 0 1 0 0 0
0 0 0 0 1 1 0
0 1 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 0 0 0 1
0 0 1 0 0 0 0];
b=ones(size(B,1),1);
for i=1:size(B,1)
while B(i,:)==0
B(i,:)=[]
b(i,:)=[]
end
end
  댓글 수: 1
Jan
Jan 2015년 10월 25일
The error is caused by running the for i loop until the original size of B, but B has been shortend already inside the loop.

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

채택된 답변

Stephen23
Stephen23 2015년 10월 25일
편집: Stephen23 2015년 10월 25일
When you use vectorized code then this task is simple, and there is no need for complicated loops:
>>B(any(B,2),:)
ans =
1 0 0 0 0 0 0
0 0 0 1 0 0 0
0 1 0 1 0 0 0
0 0 0 0 1 1 0
0 1 0 0 0 0 1
0 0 0 0 0 0 1
0 0 1 0 0 0 0
  댓글 수: 1
nadia nadi
nadia nadi 2015년 10월 25일
편집: nadia nadi 2015년 10월 25일
Dear Stephen,
That's great, thanks a lot.
Nadia

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

추가 답변 (0개)

카테고리

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