Taking An Average of Multiple Outputs From For Loop

조회 수: 11 (최근 30일)
C W
C W 2020년 4월 4일
답변: Danika 2023년 8월 10일
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?

채택된 답변

Sriram Tadavarty
Sriram Tadavarty 2020년 4월 4일
Hi Celeste,
You can take the mean of every reuterned M value, if you store the calculate M value in an array. The code overwrites the M value for each iteration it. So, update the code as below:
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(it)=(S/(length(A)));
display(M(it));
% Find variance
sum1=0;
for i=1: length(A)
sum1=sum1+ (A(i)-M(it))^2;
end
V(it)=sum1/length(A);
display(V(it));
SD(it)=sqrt(V(it));
display(SD(it));
end
% M, V, and SD contains the output of each iteration. Then, apply mean as below
meanM = mean(M);
meanSD = mean(SD);
Hope this helps.
Regards,
Sriram
  댓글 수: 2
C W
C W 2020년 4월 4일
This definitely does help, thank you. I have one other question - since I am supposed to be working on this without using the mean and SD functions, does this look correct (for the end part)?
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(it)=(S/(length(A)));
display(M(it));
% Find variance
sum1=0;
for i=1: length(A)
sum1=sum1+ (A(i)-M)^2;
end
V(it)=sum1/length(A);
display(V(it));
Variance=(V(it));
SD(it)=sqrt(V(it));
display(SD(it));
end
% M, V, and SD contains the output of each iteration. First, find the sum.
SumM = sum(M,'all');
SumSD = sum(SD, 'all');
%Divide by the number of terms to find the mean.
MeanM=(SumM/(length(M)));
%Find variance
sum1=0;
for i=1:length(Variance)
sum2=sum2+ (Variance(i)-SumM(it))^2;
end
Variance(it)=sum1/length(Variance);
display(Variance(it));
StandardDeviation(it)=sqrt(Variance(it));
display(StandardDeviation(it));
Sriram Tadavarty
Sriram Tadavarty 2020년 4월 4일
While calculating Variance(it), you used sum1, instead of sum2. You do not need to use Variance(it), instead Variance variable should be good. If you index it with it, it will form an array. I think that is not what you want.
Hope this helps.
Regards,
Sriram

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

C W
C W 2020년 4월 4일
Ok, it's starting to make sense, thank you so much! At the end, is there a way to store these values in a table?
Ex. say I do all of this, then repeat the process for a 100x100 matrix, then a 1000x1000 ... how can I generate a string of the returned means?
  댓글 수: 3
C W
C W 2020년 4월 4일
Sure, I accepted.
After I repeat this whole process using the same code but for a different matrix size, won't the variables all be overwritten?
Sriram Tadavarty
Sriram Tadavarty 2020년 4월 4일
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
Danika 2023년 8월 10일
% 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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by