Calculating Mean of Matrices
Ältere Kommentare anzeigen
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 Kommentare
Oleg Komarov
am 31 Jul. 2012
How did you name them loc1, loc2 etc...manually or did you use eval?
John
am 31 Jul. 2012
Oleg Komarov
am 31 Jul. 2012
Bearbeitet: Oleg Komarov
am 31 Jul. 2012
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
am 31 Jul. 2012
Sorry but 500x1 are vectors, not matrices and their row-means are equal to the values itselves.
Antworten (3)
per isakson
am 31 Jul. 2012
Bearbeitet: per isakson
am 31 Jul. 2012
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 Kommentare
John
am 31 Jul. 2012
Oleg Komarov
am 31 Jul. 2012
Use the structure approach or do the variables have all the same dimensions?
John
am 31 Jul. 2012
E K
am 31 Jul. 2012
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
am 31 Jul. 2012
@John
- you did not read the FAQ-entry, "A1,A2...", carefully enough
- read the documentation on MEAN
Oleg Komarov
am 31 Jul. 2012
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
am 31 Jul. 2012
Bearbeitet: Azzi Abdelmalek
am 1 Aug. 2012
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 Kommentare
Walter Roberson
am 31 Jul. 2012
Using eval() is not recommended in this context.
Azzi Abdelmalek
am 31 Jul. 2012
Bearbeitet: Azzi Abdelmalek
am 31 Jul. 2012
why, M Walter? what is better?
Oleg Komarov
am 1 Aug. 2012
@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
am 1 Aug. 2012
Bearbeitet: Azzi Abdelmalek
am 12 Sep. 2012
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
am 1 Aug. 2012
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.
Kategorien
Mehr zu Logical 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!