How can I find the mean of multiple variables in for loop

조회 수: 10 (최근 30일)
NA
NA 2022년 11월 7일
편집: John D'Errico 2022년 11월 7일
Hi,
I have 10 workspace variables (A1, A2 ....A10), each have different dimensions. I would like to find the mean of each variable but I'm not sure how to do it.
for i=1:10 %A1 ...A10
meanA(i) = mean(A); %not sure what to write here for 1:10
end
Would appreciate your help.
Thanks

답변 (2개)

John D'Errico
John D'Errico 2022년 11월 7일
편집: John D'Errico 2022년 11월 7일
You have asked 22 questions so far, and are still numbering your variables? This is yet another reason for why you need to learn MATLAB as a matrix language. Learn to use vectors and arrays of vectors, etc. DON'T name and number your variables, as if you are still using a spreadsheet. MATLAB is not a spreadsheet.
Store all of those variables in ONE array. Then it requires nothing more than one call to the function mean, to compute the mean of ALL of your vectors at once.

Matt J
Matt J 2022년 11월 7일
For example,
Arrays={rand(2), rand(3), eye(10), randn(15)}
Arrays = 1×4 cell array
{2×2 double} {3×3 double} {10×10 double} {15×15 double}
Means=cellfun(@(z)mean(z,'all') , Arrays)
Means = 1×4
0.7548 0.6077 0.1000 0.0492

카테고리

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