From: mat001 on
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.
From: Matt J on
"mat001 " <priya.biomath(a)yahoo.co.in> wrote in message <hulehc$ecr$1(a)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(:)) ?
From: John D'Errico on
"mat001 " <priya.biomath(a)yahoo.co.in> wrote in message <hulehc$ecr$1(a)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
From: Walter Roberson on
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)) ?
From: Wayne King on
"mat001 " <priya.biomath(a)yahoo.co.in> wrote in message <hulehc$ecr$1(a)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