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 中查找有关 Multidimensional Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by