Calculating Mean of Matrices

I am new to Matlab. I would like to calculate mean of many matrices(about 550 matrices. They are all the same size). I've named them loc1, loc2, loc3 .......loc549, loc550. Please help. Thanks!

4 件のコメント

Oleg Komarov
Oleg Komarov 2012 年 7 月 31 日
How did you name them loc1, loc2 etc...manually or did you use eval?
John
John 2012 年 7 月 31 日
I used eval to name them. They are all 500x1 matrices and I would like to get the means of their rows.
Oleg Komarov
Oleg Komarov 2012 年 7 月 31 日
編集済み: Oleg Komarov 2012 年 7 月 31 日
I think you're missing the point, "evaling" variables is not a good approach. You will find yourself stuck in the eval paradigm forever.
Try the other methods suggested in the FAQ that per isakson posted.
Oleg Komarov
Oleg Komarov 2012 年 7 月 31 日
Sorry but 500x1 are vectors, not matrices and their row-means are equal to the values itselves.

サインインしてコメントする。

回答 (3 件)

per isakson
per isakson 2012 年 7 月 31 日
編集済み: per isakson 2012 年 7 月 31 日

0 投票

It was probably not a good idea to name them "loc1, loc2, loc3 .......loc549, loc550". See How can I create variables A1, A2,...,A10 in a loop?.
However,
mean(loc1(:))
returns the mean of a numerical array.
Please, have a look at the Getting Started.

5 件のコメント

John
John 2012 年 7 月 31 日
Thanks for the advice. If I rename them A1,A2.....how can I calculate the means of their rows (they are all 500x1 matrices )
Oleg Komarov
Oleg Komarov 2012 年 7 月 31 日
Use the structure approach or do the variables have all the same dimensions?
John
John 2012 年 7 月 31 日
They all have the same dimensions
E K
E K 2012 年 7 月 31 日
try to use a cell structure with each cell element got one of your matrix then you can call them like C{1,1}
and with a for loop it will be very easy to find the mean of them.
per isakson
per isakson 2012 年 7 月 31 日
@John
  1. you did not read the FAQ-entry, "A1,A2...", carefully enough
  2. read the documentation on MEAN

サインインしてコメントする。

Oleg Komarov
Oleg Komarov 2012 年 7 月 31 日

0 投票

Then create one array loc, which will have the usual n by m dimension but you will be adding slices in a loop on the third dimension.
An example, suppose you are importing data (but I will be generating it):
% preallocate
A = zeros(2,4,10); % You know you will have 10 matrices
for ii = 1:10
A(:,:,ii) = rand(2,4); % one matrix per slice
end
Try to apply this example to your case, then you just have to use
mean(A,2)
mean along rows.
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 7 月 31 日
編集済み: Azzi Abdelmalek 2012 年 8 月 1 日

0 投票

for k=1:550
mat=sprintf('loc%d',k);result1=sprintf('result(%d,:)',k)
%eval([mat '=rand(2,2)']) %it's just an exemple for testing
eval([result1 '=mean(' mat ,')'])
end
% the variable result is an array with 550 lines ;each line corresponds to the mean of each matrix

5 件のコメント

Walter Roberson
Walter Roberson 2012 年 7 月 31 日
Using eval() is not recommended in this context.
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 7 月 31 日
編集済み: Azzi Abdelmalek 2012 年 7 月 31 日
why, M Walter? what is better?
Oleg Komarov
Oleg Komarov 2012 年 8 月 1 日
@Azzi: as you can see, the OP got here with this question because he used eval in the first place.
Also, your solution is not correct since you're overwriting result1.
The best solution (memory, time, legibility, safer etc...) in this case (when all results have the same dimension) is to use a 3D array.
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 8 月 1 日
編集済み: Azzi Abdelmalek 2012 年 9 月 12 日
M Komarov result1 is not overwritten, result1=sprintf('result(%d)',k) when k=2; result1='result(2)'. maby i didn't read all the question, I will remove one "mean'
Oleg Komarov
Oleg Komarov 2012 年 8 月 1 日
Ok, I agree. But, I think it is a very unhappy choice to call it result1 and assign it a dynamic string and then eval it.
Other methods are much clearer and less prone to errors.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeVariables についてさらに検索

タグ

質問済み:

2012 年 7 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by