# Mean of many changing "for loop" 3D matrices

1 visualizzazione (ultimi 30 giorni)
Patricia Klobusiakova il 29 Ott 2016
Hi,
I have 200 3D matrices with same dimensions. I'd like to create one 3d matrix that would contain mean of my 200 matrices by elements → (1,1,1) mean of 200 positions in my 3D matrices and so on ...
Here's my code:
for tt = 1:200;
M = ... (tt) loads the 3D data
end
mean()
Thanks.
##### 3 CommentiMostra 2 commenti meno recentiNascondi 2 commenti meno recenti
Patricia Klobusiakova il 30 Ott 2016
for ii = 1:nsub
for tt = 1:200;
end
mean(M,4)

Accedi per commentare.

### Risposta accettata

dpb il 30 Ott 2016
Well, you could do that (store in a 4D array, that is) but that requires memory for all 200 at once and there's no need when computing a mean to have all the data at once...just accumulate the totals and then divide after the loop is finished...
for tt = 2:200
end
M=M/200; % average
##### 1 CommentoMostra NessunoNascondi Nessuno
Patricia Klobusiakova il 30 Ott 2016
That's cool. I didn't think of that. Thank you very much!

Accedi per commentare.

### Più risposte (1)

Kwin il 30 Ott 2016
If you have your matrices all stored in one (four dimensional) matrix, for example M(:,:,:,k) would return the kth 3D matrix, but another order of the index would be fine as well, such as M(k,:,:,:), then, as dpb already mentioned, you can get the mean by simply using:
mean(M,location_of_k)
so for example for M(:,:,:,k), location_of_k=4 and for M(k,:,:,:), location_of_k=1.
##### 1 CommentoMostra NessunoNascondi Nessuno
Patricia Klobusiakova il 30 Ott 2016
Thanks for your answer! Could you take a look on the comment I wrote under my question? The problem is I am not able to create a 4D matrix because of a for loop I have in the code.

Accedi per commentare.

### Categorie

Scopri di più su Resizing and Reshaping Matrices in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!