Taking An Average of Multiple Outputs From For Loop
Ältere Kommentare anzeigen
I am generating a for loop which is able to take the average and SD of randomly generated 10x10 matrices 10 times (without using mean and std) with the following:
for it = 1:10
A=randn(10);
% Calculate Sum
S=sum(A,'all');
% Divide by the Number N in the Matrix to Find the Mean
M=(S/(length(A)));
display(M);
% Find variance
sum1=0;
for i=1: length(A)
sum1=sum1+ (A(i)-M)^2;
end
V=sum1/length(A);
display(V);
SD=sqrt(V);
display(SD);
end
I need to be able to calculate the AVERAGE mean and SD of the 10 outputs without doing it by hand. I tried to do this by attempting to take mean(M) etc. and it gives only the last number. How can I take the mean of every returned M value?
Akzeptierte Antwort
Weitere Antworten (2)
C W
am 4 Apr. 2020
0 Stimmen
3 Kommentare
Sriram Tadavarty
am 4 Apr. 2020
Hi, Please do accept the answer, if helped.
I didn't understand the clause "is there a way to store these values in a table?". These values are already stored in the respective variables.
C W
am 4 Apr. 2020
Sriram Tadavarty
am 4 Apr. 2020
Yes, they will be overwritten. To perform that task, you can use another for loop on top this for each size of matrix, and then store the variables in cell array with each element corresponding to different size of matrix.
Danika
am 10 Aug. 2023
0 Stimmen
% Variables
x = [1.8 3.6 5.4 7.2];
fprintf('x =');
disp(x);
% For Loop
for k = 1:length(x);
fprintf('Element %0.0f is: %0.1f \n', k, x(k));
end
Kategorien
Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!