How to find mean from different variables in work space

조회 수: 7 (최근 30일)
Safi ullah
Safi ullah 2017년 12월 6일
댓글: Safi ullah 2017년 12월 6일
Hello, everyone I have 91 variables in workspace. The size of all variables is same e.g
Z1=133×8
Z2=133×8
Z3=133×8
.........
.........
Z91=133×8
I am trying to find the mean of rows (35-36) for each variable by using for loop given below
for i=1:91;
answer=mean(zi(35:36,:));
end
but it does not work. Any guidance will be appreciated. Thanks
  댓글 수: 1
Stephen23
Stephen23 2017년 12월 6일
편집: Stephen23 2017년 12월 6일
The simplest solution is do not have 90 variables in your workspace and then try to perform calculations on them all. MATLAB is really efficient when operating on arrays, and if you had just one array then all you would need is one call to mean. How simple can it get?
If you get all of variables by importing some data then import them into one ND array and your task will be trivial to solve. Currently you have a task that can only be solved by writing ugly, complex, slow, buggy, hard-to-debug code.

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

채택된 답변

KL
KL 2017년 12월 6일
why do you have 91 variables named sequentially in the workspace? Why not use a 3D matrix?!
You only need one variable called Z.
Z = rand(133,8,91);
then to calculate mean, all you need is one line,
Z_m = mean(A(35:36,:,:))
  댓글 수: 2
Jan
Jan 2017년 12월 6일
+1. Of course. Using "Z1, Z2, ..." is the problem. As soon as a cell array or multi-dimensional array is used, the calculation of the mean gets trivial.
Safi ullah
Safi ullah 2017년 12월 6일
@ Stephen Cobeldick , Jan Simon and KL thank all of you for guidance. @ KL I checked your code it works well.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by