Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

delete empty - zeros row in a matrix

2,348 views
Skip to first unread message

Nir Sh

unread,
Mar 20, 2009, 6:38:01 AM3/20/09
to
hi,
i have larg matrix (120000*128). some of the rows are zeros rows (contain only zeros ) and some are not (contain zeros and non zeros).
how can i remove rows that contain only zeros ?
thank's in advance.

us

unread,
Mar 20, 2009, 6:47:01 AM3/20/09
to
"Nir Sh"
> i have larg matrix (120000*128). some of the rows are zeros rows (contain only zeros ) and some are not (contain zeros and non zeros).
> how can i remove rows that contain only zeros ...

one of the solutions

m=[
1 0 0
1 2 3
0 0 0
0 1 0
0 0 0
];
m(all(m==0,2),:)=[]
%{
% m =
1 0 0
1 2 3
0 1 0
%}

us

Lothar Schmidt

unread,
Mar 20, 2009, 6:46:48 AM3/20/09
to
Nir Sh schrieb:

%generate random matrix
a=rand(4)
% generate row with zeros
a(3,:)=0
% select nonzero rows
b=a(sum(a')~=0;:)

us

unread,
Mar 20, 2009, 6:54:01 AM3/20/09
to
Lothar Schmidt

> % select nonzero rows
> b=a(sum(a')~=0;:)

well...
even after correcting the syntax error...

a=[
1 -1 % <- sum := 0
0 0
2 2
];
b=a(sum(a')~=0,:) % <- chage ; to ,
% b = 2 2

us

Lothar Schmidt

unread,
Mar 20, 2009, 6:59:58 AM3/20/09
to
us schrieb:

aargnnnn! you're right! i'm not...

the only excuse: my example is much more probable than yours :-)

Tim Love

unread,
Mar 20, 2009, 7:17:56 AM3/20/09
to
"Nir Sh" <nnii...@yahoo.com> writes:

Try searching in google for
matlab how can i remove rows that contain only zeros

Eck Lesi

unread,
May 27, 2009, 7:41:01 AM5/27/09
to
"Nir Sh" <nnii...@yahoo.com> wrote in message <gpvrm9$kt1$1...@fred.mathworks.com>...

you can do it in 2 simple steps:
1. take the original "large matrix (120000*128)", let's call it A, and project onto a one single column, with

B=sum(A,2);

The vector B will have 0s exactly at the locations corresponding to the zero rows of A.
So now:

2. a=find(B>0);

C=A(a,:)

will get rid of the zero rows in A


Steven Lord

unread,
May 27, 2009, 10:04:39 AM5/27/09
to

"Eck Lesi" <spam_can...@yahoo.com> wrote in message
news:gvj8sd$mha$1...@fred.mathworks.com...

> "Nir Sh" <nnii...@yahoo.com> wrote in message
> <gpvrm9$kt1$1...@fred.mathworks.com>...
>> hi,
>> i have larg matrix (120000*128). some of the rows are zeros rows (contain
>> only zeros ) and some are not (contain zeros and non zeros).
>> how can i remove rows that contain only zeros ?
>> thank's in advance.
>
> you can do it in 2 simple steps:
> 1. take the original "large matrix (120000*128)", let's call it A, and
> project onto a one single column, with
>
> B=sum(A,2);
>
> The vector B will have 0s exactly at the locations corresponding to the
> zero rows of A.

Counterexample:

A = [1 2 3 4;5 6 7 -18]

You want sum(abs(A), 2) or ~any(A, 2) or all(A==0, 2) instead.

*snip*

--
Steve Lord
sl...@mathworks.com


Eck Lesi

unread,
May 27, 2009, 7:20:17 PM5/27/09
to

OK, add one more step A=abs(A) at the beginning to get rid of negative values.
Although I woudn't even bother with that if the matrix A is generic.

Eck Lesi

unread,
May 27, 2009, 8:34:02 PM5/27/09
to
0 new messages