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:
Matrix average problem

Subject: Matrix average problem

From: mat001

Date: 8 Jun, 2010 12:54:04

Message: 1 of 8

I have a 2d matrix called A which has most of the element is zero and i want to find average of matrix

I use

Average = sum(nonzeros(A(:)))/numel(nonzeros(A(:)));

Is it okey?

It seems to me okey but problem is i choose maximum value in matrix A is 20 so the average should be also maximum 20.

If one could undersatnd this then please help me to find error
Because I am getting more than 20 for my average.

Subject: Matrix average problem

From: Matt J

Date: 8 Jun, 2010 13:09:04

Message: 2 of 8

"mat001 " <priya.biomath@yahoo.co.in> wrote in message <hulehc$ecr$1@fred.mathworks.com>...
> I have a 2d matrix called A which has most of the element is zero and i want to find average of matrix
>
> I use
>
> Average = sum(nonzeros(A(:)))/numel(nonzeros(A(:)));
>
> Is it okey?
==================

It's fine, but you don't need the (:) for nonzeros and it's a bit inefficient to compute
nonzeros(A) twice. Would be better to do

nza=nonzeros(A);
Average=sum(nza)/numel(nza);

===================
> It seems to me okey but problem is i choose maximum value in matrix A is 20 so the average should be also maximum 20.
>
> If one could undersatnd this then please help me to find error
> Because I am getting more than 20 for my average.
==================

Perhaps the maximum in A is not really 20. Many coding errors could cause this.
Did you actually check the output of max(A(:)) ?

Subject: Matrix average problem

From: John D'Errico

Date: 8 Jun, 2010 13:10:20

Message: 3 of 8

"mat001 " <priya.biomath@yahoo.co.in> wrote in message <hulehc$ecr$1@fred.mathworks.com>...
> I have a 2d matrix called A which has most of the element is zero and i want to find average of matrix
>
> I use
>
> Average = sum(nonzeros(A(:)))/numel(nonzeros(A(:)));
>
> Is it okey?
>
> It seems to me okey but problem is i choose maximum value in matrix A is 20 so the average should be also maximum 20.
>
> If one could undersatnd this then please help me to find error
> Because I am getting more than 20 for my average.

What you did wrong is difficult to know, since
you don't show us the matrix to know that your
claims are true.

You could simply do this however:

mean(nonzeros(A))

John

Subject: Matrix average problem

From: Walter Roberson

Date: 8 Jun, 2010 13:22:18

Message: 4 of 8

mat001 wrote:
> I have a 2d matrix called A which has most of the element is zero and i
> want to find average of matrix
>
> I use
>
> Average = sum(nonzeros(A(:)))/numel(nonzeros(A(:)));
> Is it okey?

Why not use mean(nonzeros(A)) ?

Subject: Matrix average problem

From: Wayne King

Date: 8 Jun, 2010 13:30:20

Message: 5 of 8

"mat001 " <priya.biomath@yahoo.co.in> wrote in message <hulehc$ecr$1@fred.mathworks.com>...
> I have a 2d matrix called A which has most of the element is zero and i want to find average of matrix
>
> I use
>
> Average = sum(nonzeros(A(:)))/numel(nonzeros(A(:)));
>
> Is it okey?
>
> It seems to me okey but problem is i choose maximum value in matrix A is 20 so the average should be also maximum 20.
>
> If one could undersatnd this then please help me to find error
> Because I am getting more than 20 for my average.

Hi, it looks fine to me:

reset(RandStream.getDefaultStream);
A = zeros(20,20);
indices = randi([1 400],40,1);
reset(RandStream.getDefaultStream);
A(indices) = randi([1 20],40,1);
Average = sum(nonzeros(A))/numel(nonzeros(A));
% same as
mean(nonzeros(A))
% mean is 11.5789

Are you sure that max(nonzeros(A)) does not exceed 20?

nonzeros(A) should just be a column vector, so call mean() on that.

Wayne

Subject: Matrix average problem

From: Loren Shure

Date: 8 Jun, 2010 15:21:14

Message: 6 of 8

In article <hulehc$ecr$1@fred.mathworks.com>, priya.biomath@yahoo.co.in
says...
> I have a 2d matrix called A which has most of the element is zero and i want to find average of matrix
>
> I use
>
> Average = sum(nonzeros(A(:)))/numel(nonzeros(A(:)));
>
> Is it okey?
>
> It seems to me okey but problem is i choose maximum value in matrix A is 20 so the average should be also maximum 20.
>
> If one could undersatnd this then please help me to find error
> Because I am getting more than 20 for my average.
>

Shouldn't your mean account for having zeros as well? Why not

mean(A(:))?

--
Loren
http://blogs.mathworks.com/loren
http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Matrix average problem

From: mat001

Date: 9 Jun, 2010 09:45:09

Message: 7 of 8

Loren Shure <loren.shure@mathworks.com> wrote in message <MPG.267829f6a173c605989af3@news.mathworks.com>...
> In article <hulehc$ecr$1@fred.mathworks.com>, priya.biomath@yahoo.co.in
> says...
> > I have a 2d matrix called A which has most of the element is zero and i want to find average of matrix
> >
> > I use
> >
> > Average = sum(nonzeros(A(:)))/numel(nonzeros(A(:)));
> >
> > Is it okey?
> >
> > It seems to me okey but problem is i choose maximum value in matrix A is 20 so the average should be also maximum 20.
> >
> > If one could undersatnd this then please help me to find error
> > Because I am getting more than 20 for my average.
> >
>
> Shouldn't your mean account for having zeros as well? Why not
>
> mean(A(:))?
>
> --
> Loren
> http://blogs.mathworks.com/loren
> http://matlabwiki.mathworks.com/MATLAB_FAQ

If i will consider those zeros then I will never get good average.
Because all the element are zero near all boundaries. only values are available in the middle

like

average of

0 0 0 5 5 5 0 0 0

will be 15/9

but i need average over nonzero
e.g
15/5

Subject: Matrix average problem

From: Jos (10584)

Date: 9 Jun, 2010 09:58:06

Message: 8 of 8

"mat001 " <priya.biomath@yahoo.co.in> wrote in message <hulehc$ecr$1@fred.mathworks.com>...
> I have a 2d matrix called A which has most of the element is zero and i want to find average of matrix
>
> I use
>
> Average = sum(nonzeros(A(:)))/numel(nonzeros(A(:)));
>
> Is it okey?
>
> It seems to me okey but problem is i choose maximum value in matrix A is 20 so the average should be also maximum 20.
>
> If one could undersatnd this then please help me to find error
> Because I am getting more than 20 for my average.

Two improvements:

1) sum(nonzeros(A(:))) is, almost by definition of zero, the same as sum(A(:))
2) numel(nonzeros(A)) can be replaced by NNZ

Average = sum(A(:)) / nnz(A)

hth
Jos

Tags for 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