How can i delete rows with zeros from a matrix and rebuild the matrix?

9 views (last 30 days)
I have this matrix, as an example, because the problem is a nxm one with a more complex program, so i hope this may help.
>> A=rand(9,3)
A =
0.575208595078466 0.73172238565867 0.368484596490336
0.0597795429471558 0.647745963136307 0.62561856072969
0.234779913372406 0.450923706430945 0.780227435151377
0.353158571222071 0.547008892286345 0.0811257688657853
0.821194040197959 0.296320805607773 0.92938597096873
0.0154034376515551 0.744692807074156 0.775712678608402
0.0430238016578078 0.188955015032545 0.486791632403172
0.168990029462704 0.686775433365315 0.435858588580919
0.649115474956452 0.18351115573727 0.446783749429806
>> B=[A;zeros(1,3)]
B =
0.575208595078466 0.73172238565867 0.368484596490336
0.0597795429471558 0.647745963136307 0.62561856072969
0.234779913372406 0.450923706430945 0.780227435151377
0.353158571222071 0.547008892286345 0.0811257688657853
0.821194040197959 0.296320805607773 0.92938597096873
0.0154034376515551 0.744692807074156 0.775712678608402
0.0430238016578078 0.188955015032545 0.486791632403172
0.168990029462704 0.686775433365315 0.435858588580919
0.649115474956452 0.18351115573727 0.446783749429806
0 0 0
>> C=[0:9]
C =
0 1 2 3 4 5 6 7 8 9
>> D=C'
D =
0
1
2
3
4
5
6
7
8
9
>> E=[D,B]
E =
0 0.575208595078466 0.73172238565867 0.368484596490336
1 0.0597795429471558 0.647745963136307 0.62561856072969
2 0.234779913372406 0.450923706430945 0.780227435151377
3 0.353158571222071 0.547008892286345 0.0811257688657853
4 0.821194040197959 0.296320805607773 0.92938597096873
5 0.0154034376515551 0.744692807074156 0.775712678608402
6 0.0430238016578078 0.188955015032545 0.486791632403172
7 0.168990029462704 0.686775433365315 0.435858588580919
8 0.649115474956452 0.18351115573727 0.446783749429806
9 0 0 0
>>
My question is how can transform matrix E into
E =
0 0.575208595078466 0.73172238565867 0.368484596490336
1 0.0597795429471558 0.647745963136307 0.62561856072969
2 0.234779913372406 0.450923706430945 0.780227435151377
3 0.353158571222071 0.547008892286345 0.0811257688657853
4 0.821194040197959 0.296320805607773 0.92938597096873
5 0.0154034376515551 0.744692807074156 0.775712678608402
6 0.0430238016578078 0.188955015032545 0.486791632403172
7 0.168990029462704 0.686775433365315 0.435858588580919
8 0.649115474956452 0.18351115573727 0.446783749429806
in order to print it and to write it in a txt file easily without the zeros. And remember, as this is an nxm matrix it can be also...
E =
0 0.575208595078466 0.73172238565867 0.368484596490336
1 0.0597795429471558 0.647745963136307 0.62561856072969
2 0.234779913372406 0.450923706430945 0.780227435151377
3 0.353158571222071 0.547008892286345 0.0811257688657853
4 0.821194040197959 0.296320805607773 0.92938597096873
5 0.0154034376515551 0.744692807074156 0.775712678608402
6 0.0430238016578078 0.188955015032545 0.486791632403172
7 0.168990029462704 0.686775433365315 0.435858588580919
8 0.649115474956452 0.18351115573727 0.446783749429806
9 0 0 0
10 0 0 0
Also, i would like to transform a matrix like
E =
0 0.575208595078466 0.73172238565867 0.368484596490336
1 0.0597795429471558 0.647745963136307 0.62561856072969
2 0.234779913372406 0.450923706430945 0.780227435151377
3 0.353158571222071 0.547008892286345 0.0811257688657853
4 0.821194040197959 0.296320805607773 0.92938597096873
5 0.0154034376515551 0.744692807074156 0.775712678608402
6 0.0430238016578078 0.188955015032545 0.486791632403172
7 0.168990029462704 0.686775433365315 0.435858588580919
8 0.649115474956452 0.18351115573727 0.446783749429806
0 0 0 0
0 0 0 0
in the same as before.
E =
0 0.575208595078466 0.73172238565867 0.368484596490336
1 0.0597795429471558 0.647745963136307 0.62561856072969
2 0.234779913372406 0.450923706430945 0.780227435151377
3 0.353158571222071 0.547008892286345 0.0811257688657853
4 0.821194040197959 0.296320805607773 0.92938597096873
5 0.0154034376515551 0.744692807074156 0.775712678608402
6 0.0430238016578078 0.188955015032545 0.486791632403172
7 0.168990029462704 0.686775433365315 0.435858588580919
8 0.649115474956452 0.18351115573727 0.446783749429806
Thank you very much.

Answers (2)

dbmn
dbmn on 12 Jul 2017
A not so easily readable solution would be the following
% Create a zero vector to compare to
a=zeros(1,size(E,2)-1);
% Compare to that vector and check if each element is the same
index = [sum([E(:,2:end)==a]')==(size(E,2)-1)]';
% delete all the rows that are the same
E(index, :)=[];
  2 Comments
Pablo Álvarez Rodríguez
Pablo Álvarez Rodríguez on 12 Jul 2017
I will have to try this one for the zero cases, thanks... bit i think it won't help the numbered one
dbmn
dbmn on 13 Jul 2017
With the numbered one it should work as well, because the first row is not taken into consideration at all with E(:, 2:end). Hence it does not matter what is written in the first column.
Geoff`s solution is more beautiful than mine - so you might want to combine the two

Sign in to comment.


Geoff Hayes
Geoff Hayes on 12 Jul 2017
Pablo - you could try the following but it assumes that equality with zero will always return true (which isn't necessarily the case unless you are using integers)
A = rand(9,3);
A = [A ; zeros(1,3)];
A(all(A==0,2),:) = [];
In the above, we use all to find all rows of A that have all zeros. all(A==0,2) will return a column array of zeros and ones where a one indicates all zeros in that row. We then use that column array to set all zero rows to the empty array which will remove the row from A.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!