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에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!