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.
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Multidimensional Arrays 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!