How to find the mean of a set of numbers without using the mean or sum functions

For an assignment I need to write the function to find the mean of a set of numbers without using the mean or sum functions however I can only seem to get the mean of a row at a time with this if I enter a matrix that has more than one row and I can't figure out to total all the numbers in the rows and columns:
% code
function v=w2q1ii(m)
%my_mean(v) finds the mean (m) of a set of numbers
v = input('Enter values as array : ');
s=size(v,1)
q=size(v,2)
for i=1:s
row=v(i,:);
total=0;
for j=1:q
total=total+row(j)
end
fprintf(1,['Sum of elements %.0f = ',num2str(total),'\j'],i);
end
m=total/numel(v)
end
This is what I have so far, can anybody shed some light on what i'm doing wrong?

1 Comment

You are disregarding the results for each row. You need to store them somewhere and then loop again to get the mean of all rows. Alternatively you could use something like:
totMean = totMean + rowMean/numRows
inside your loop. The mean would need to be initialized to zero outside your loop.

Sign in to comment.

 Accepted Answer

Probably not what your teacher wants, but an opportunity to learn about convolution:
nRow = 10;
nCol = 15;
your_mat = rand(nRow,nCol);
your_mean = conv2(your_mat,ones(nRow,nCol)./(nRow*nCol), 'valid')

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 29 Jan 2014

Edited:

on 29 Jan 2014

Community Treasure Hunt

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

Start Hunting!