Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
nnz

Subject: nnz

From: Wendy

Date: 8 Dec, 2010 04:42:05

Message: 1 of 8

Hi all,

I have a matrix, such as

a =

     1 1 1 1 0
     1 1 1 1 1
     1 0 0 1 0

I want to count the number of nonzero elements in each row. Is there any matrix function that I can use instead of using nnz for each row? The output that I expect is

output =
4
5
2

Thanks very much,
Wendy

Subject: nnz

From: Nicolas Cusseau

Date: 12 Dec, 2010 21:08:04

Message: 2 of 8

"Wendy " <wlq121@gmail.com> wrote in message <idn2at$bmf$1@fred.mathworks.com>...
> Hi all,
>
> I have a matrix, such as
>
> a =
>
> 1 1 1 1 0
> 1 1 1 1 1
> 1 0 0 1 0
>
> I want to count the number of nonzero elements in each row. Is there any matrix function that I can use instead of using nnz for each row? The output that I expect is
>
> output =
> 4
> 5
> 2
>
> Thanks very much,
> Wendy

Hi,
It's a bit twiddling hack, so you have to write your own mex-file.
If your matrixstores only binary digit, you can still use:

output = sum(a,2);

Regards,
Nico

Subject: nnz

From: John D'Errico

Date: 12 Dec, 2010 21:21:04

Message: 3 of 8

"Nicolas Cusseau" <cusseani@ensieta.fr> wrote in message <ie3djk$hr3$1@fred.mathworks.com>...
> "Wendy " <wlq121@gmail.com> wrote in message <idn2at$bmf$1@fred.mathworks.com>...
> > Hi all,
> >
> > I have a matrix, such as
> >
> > a =
> >
> > 1 1 1 1 0
> > 1 1 1 1 1
> > 1 0 0 1 0
> >
> > I want to count the number of nonzero elements in each row. Is there any matrix function that I can use instead of using nnz for each row? The output that I expect is
> >
> > output =
> > 4
> > 5
> > 2
> >
> > Thanks very much,
> > Wendy
>
> Hi,
> It's a bit twiddling hack, so you have to write your own mex-file.
> If your matrixstores only binary digit, you can still use:
>
> output = sum(a,2);
>
> Regards,
> Nico

Why in the name of god do you need to write a MEX
function to do something trivial?

numberzeros = sum(a ~= 0,2);

John

Subject: nnz

From: Nicolas Cusseau

Date: 12 Dec, 2010 22:30:22

Message: 4 of 8

"John D'Errico" <woodchips@rochester.rr.com> wrote in message <ie3ec0$8p$1@fred.mathworks.com>...
> "Nicolas Cusseau" <cusseani@ensieta.fr> wrote in message <ie3djk$hr3$1@fred.mathworks.com>...
> > "Wendy " <wlq121@gmail.com> wrote in message <idn2at$bmf$1@fred.mathworks.com>...
> > > Hi all,
> > >
> > > I have a matrix, such as
> > >
> > > a =
> > >
> > > 1 1 1 1 0
> > > 1 1 1 1 1
> > > 1 0 0 1 0
> > >
> > > I want to count the number of nonzero elements in each row. Is there any matrix function that I can use instead of using nnz for each row? The output that I expect is
> > >
> > > output =
> > > 4
> > > 5
> > > 2
> > >
> > > Thanks very much,
> > > Wendy
> >
> > Hi,
> > It's a bit twiddling hack, so you have to write your own mex-file.
> > If your matrixstores only binary digit, you can still use:
> >
> > output = sum(a,2);
> >
> > Regards,
> > Nico
>
> Why in the name of god do you need to write a MEX
> function to do something trivial?
>
> numberzeros = sum(a ~= 0,2);
>
> John


Hi,

I don't know how god is related to Matlab but when Wendy ask the newsgroup how to vectorize nnz, I think it is in order to gain speed. Otherwise, a simple for-loop would be enough.

Since "nnz" is based on the "find" built-in function, the solution "sum(a ~=0,2)" should be less efficient than a for-loop using nnz.

So, it should be fun for anybody to learn some hacks in C and to develop its own C functions.
Here is an interesting link:
http://www-graphics.stanford.edu/~seander/bithacks.html

As far as I know, nothing is trivial in Matlab :)

Regards,
Nicolas

Subject: nnz

From: Oleg Komarov

Date: 12 Dec, 2010 22:45:04

Message: 5 of 8

> I don't know how god is related to Matlab but when Wendy ask the newsgroup
> how to vectorize nnz, I think it is in order to gain speed. Otherwise, a simple for-loop > would be enough.

Wendy didn't ask to vectorize nnz.

> Since "nnz" is based on the "find" built-in function, the solution "sum(a ~=0,2)"
> should be less efficient than a for-loop using nnz.

Sum is a built in as well, and it is faster:

a = [1 1 1 1 0
     1 1 1 1 1
     1 0 0 1 0];

 tic
 out1 = sum(a == 1, 2);
 toc
 
 tic
 out2 = zeros(3,1);
 for ii = 1:3
    out2(ii) = nnz(a(ii,:));
 end
 toc
 
> So, it should be fun for anybody to learn some hacks in C and to develop its own C functions.
> Here is an interesting link:
> http://www-graphics.stanford.edu/~seander/bithacks.html
>
> As far as I know, nothing is trivial in Matlab :)
>
> Regards,
> Nicolas

I agree on the last two statements.

Oleg

Subject: nnz

From: Bruno Luong

Date: 12 Dec, 2010 22:50:29

Message: 6 of 8

"Nicolas Cusseau" <cusseani@ensieta.fr> wrote in message <ie3idu$41n$1@fred.mathworks.com>...

>
> Since "nnz" is based on the "find" built-in function, the solution "sum(a ~=0,2)" should be less efficient than a for-loop using nnz.
>

I don't follow your argument, within one sentence there are four things that bother me:

1) both FIND and SUM are built-in function.
2) How do you know nnz uses FIND?
3) Even if it's true For-loop will copy a new column matrix each step which I suspect is a costly operation.
4) Did you time before claiming SUM solution is slower than NNZ/FIND?

Nothing is evidence indeed.

Bruno

Subject: nnz

From: Paulo Silva

Date: 12 Dec, 2010 23:11:03

Message: 7 of 8

my own function, just not very efficient :(

a =[1 1 1 1 0;1 1 1 1 1;1 0 0 1 0];
l=size(a);
b=zeros(l(1),1);
for c=1:l(1)
    num=0;
    for d=1:l(2)
       if (a(c,d)~=0)
           num=num+1;
       end
    end
b(c)=num;
end
b

Subject: nnz

From: Nicolas Cusseau

Date: 12 Dec, 2010 23:47:05

Message: 8 of 8

"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <ie3jjl$lnd$1@fred.mathworks.com>...
> "Nicolas Cusseau" <cusseani@ensieta.fr> wrote in message <ie3idu$41n$1@fred.mathworks.com>...
>
> >
> > Since "nnz" is based on the "find" built-in function, the solution "sum(a ~=0,2)" should be less efficient than a for-loop using nnz.
> >
>
> I don't follow your argument, within one sentence there are four things that bother me:
>
> 1) both FIND and SUM are built-in function.
> 2) How do you know nnz uses FIND?
> 3) Even if it's true For-loop will copy a new column matrix each step which I suspect is a costly operation.
> 4) Did you time before claiming SUM solution is slower than NNZ/FIND?
>
> Nothing is evidence indeed.
>
> Bruno



Hi,

You are all right Bruno.

I have developed some small functions based on the website I have quoted in my previous post to deal with data compression and I rely too much on these functions.
I should time before I post, I apologize.

Sorry for the "nnz and find" topic too.
I meant:
"Since John's "nnz" is based on the "find" built-in function, the solution "sum(a ~=0,2)" should be less efficient than a for-loop using nnz."

My point about Wendy's question was that nnz is a classical hack for C language. It is not a waste of time to develop your own mex-files and it was a very good exercise (at least for me).

By the way, Wendy's matrix is only filled with 1's and 0's.

numberzeros = sum(a ~= 0,2); is not useful in this situation.
sum(a,2) was enough.

If you want to compare :

%%%%%%%%%%%%%%%%%

% Solution 1

 tic
 out2 = zeros(3,1);
 for ii = 1:3
    out2(ii) = nnz(a(ii,:));
 end
 toc

% Solution 2

tic
out1 = sum(a == 1, 2);
toc

% Solution 3

tic
out1 = sum(a, 2);
toc

%%%%%%%%%%%%%%%%%
Elapsed time is 0.000074 seconds.
Elapsed time is 0.000060 seconds.
Elapsed time is 0.000021 seconds.

Why should we use "find"?

I just wanted to help Wendy since the "nnz" post is pretty old :).

Regards,

Nicolas

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us