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

조회 수: 15 (최근 30일)
Pablo Álvarez Rodríguez
Pablo Álvarez Rodríguez 2017년 7월 12일
댓글: dbmn 2017년 7월 13일
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.

답변 (2개)

dbmn
dbmn 2017년 7월 12일
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
Pablo Álvarez Rodríguez
Pablo Álvarez Rodríguez 2017년 7월 12일
I will have to try this one for the zero cases, thanks... bit i think it won't help the numbered one
dbmn
dbmn 2017년 7월 13일
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

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


Geoff Hayes
Geoff Hayes 2017년 7월 12일
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.
  댓글 수: 1
Pablo Álvarez Rodríguez
Pablo Álvarez Rodríguez 2017년 7월 12일
I am using integers for indexing the rows (first column)
It may help in the case of all rows as zero.

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by