# How to average multiple cells into new cell?

3 views (last 30 days)
reyadh Albarakat on 2 Dec 2016
Answered: Guillaume on 2 Dec 2016
Hi all,
I have 39 cells each one has matrix (317*202). I want to get the average of all cells in new cell with same dimension (317*202). Thank you in advance
Here is the code
cd F:\NDWI_INDEXes\VNDWI\MOD09A1_HU\VNDWI_HU_2000;
vndwi_HU{i}(vndwi_HU{i}>1)=NaN;
vndwi_HU{i}(vndwi_HU{i}<0)=NaN;
vndwi_HU{i}(vndwi_HU{i}==0)=NaN;
end

Guillaume on 2 Dec 2016
You would be much better off using a 3D matrix (of size 317*202*numel(F_read)) instead of a cell array for storage. This is also the way to getting what you want:
vndwi_HUmat = cat(3, vndwi_HUmat{:}); %convert cell array into 3D matrix
vndwi_mean = mean(vndwi_HUmat, 3); %and get the average across 3rd dimension == across cells
Your thresholding operation would have been easier that way. After the loop you just had to do:
vndwi_MUmat(vndwi_MUmat > 1 | vndwi_MUmat <= 0) = NaN;

### Categories

Find more on Cell Arrays in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by