Write a MATLAB Mfile named ColAvg which will Accept any inp
Write a MATLAB M-file named ColAvg , which will
Accept any input matrix, and report the column averages
Returns as output the average of all the elements in the matrix.
The averages should be the same as using MATLAB built-in function mean , but do not use this function.
Hint : The code for this problem will be similar to sumonlypos.m found in your textbook on page 160.
Example
Solution
% matlab code average of matrix column and total average
function avg = ColAvg(A)
[row, column] = size(A);
avg = 0;
for i = 1:column
colavg = 0.0;
for j = 1:row
colavg = colavg + A(j,i);
avg = avg + A(j,i);
end
colavg = colavg/row;
fprintf(\"The average of column %d is %f\ \",i,colavg);
end
avg = avg/(row*column);
end
A = input(\"Input Matrix: \");
ans = ColAvg(A);
fprintf(\"ans = %f\ \",ans);
%{
output:
The average of column 1 is 5.3333
The average of column 2 is -3.3333
The average of column 3 is -1.6667
The average of column 4 is -5.6667
ans = -1.3333
%}
