how to delete a row from a matrix

2 Ansichten (letzte 30 Tage)
nadia nadi
nadia nadi am 25 Okt. 2015
Bearbeitet: Stephen23 am 25 Okt. 2015
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 Kommentar
Jan
Jan am 25 Okt. 2015
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 25 Okt. 2015
Bearbeitet: Stephen23 am 25 Okt. 2015
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 Kommentar
nadia nadi
nadia nadi am 25 Okt. 2015
Bearbeitet: nadia nadi am 25 Okt. 2015
Dear Stephen,
That's great, thanks a lot.
Nadia

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by