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

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.

댓글을 달려면 로그인하십시오.

 채택된 답변

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')

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2014년 1월 29일

편집:

2014년 1월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by