# How to take mean of 3D matrices without storing them

1 vue (au cours des 30 derniers jours)
nlm le 27 Mai 2021
Commenté : Jonas le 28 Mai 2021
Hi,
I have 31 matrixes of 18000 by 36000 double type. The memory doesn't allow me to store more than 3 matrixes at once to take mean.
How can I take the mean of 31 matrixes without storing them ?
all = [];
for KK = 1:31
all = cat(3,all,data);
end
all_mean = nanmean(all,3);
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Réponse acceptée

Walter Roberson le 28 Mai 2021
%assuming that your data is currently in a cell array
total = zeros(size(data{1}));
validcounts = total;
for KK = 1:length(data)
mat = data{K};
total = total + mat;
end
all_mean = total ./ validcounts;
Note: any location that was nan in all the matrices will be nan in all_mean.
##### 0 commentairesAfficher -2 commentaires plus anciensMasquer -2 commentaires plus anciens

Connectez-vous pour commenter.

### Plus de réponses (1)

Jonas le 27 Mai 2021
sum them up one by one, this way you have one big summing matrix and the current loaded matrix. after summing you can divide all matrix elements by 31. if there really exist NaN in your matrix you have to create a third big matrix which counts for each entry if the value was NaN or not which results into a matrix with highest value 31 (at the end you then divide element wise by that matrix instead of all elements through 31). at the same time you have to set NaN entries to 0 before you add them
##### 2 commentairesAfficher AucuneMasquer Aucune
nlm le 27 Mai 2021
using a for loop ?
Jonas le 28 Mai 2021
just like in the answer of Walter. only his assumption that all data is on a cell array may be wrong because you said you could not have more than 3 matrices in your workspace. depending on how you access your data you have to load maybe one file after the other and remove previously loaded data

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by