Calculating average of cell arrays

Hello all,
I have a 1 x 3 cell which represents monthly data from 1989 to 2016 for 3 models (each cell arrays is for one model). I want to calculate average value of the precip column of these 3 models for monthly 1989 to 2016. So the output will be a 1 x 1 cell which contains the average value of precip in all models from 1989 to 2016 (monthly). It is so hard for me and I tried some codes but they are not accomplished well.
This is a sample data sets and I have more than 100 cell arrays which some of them ends in 2018 instead of 2016.
Thank you so much.

 Accepted Answer

dpb
dpb on 16 Apr 2020
Edited: dpb on 17 Apr 2020
mnTemp=cellfun(@(t) mean(t.precip),SAMPLE);
with the sample dataset:
>> cellfun(@(t) mean(t.precip),SAMPLE)
ans =
27.8985 62.4198 43.8376
>>
Don't provide any details on how these data/models were created but would be much easier to combine the various data into one table or timetable with the column variables representing the models.
ADDENDUM:
It's still the wrong way to store the data, but...
mnTemp=mean(cell2mat(cellfun(@(t) (t.precip),SAMPLE,'UniformOutput',false)),2);

3 Comments

BN
BN on 16 Apr 2020
Edited: BN on 17 Apr 2020
Dear dpb,
First I want to thank you. Unfortunately, this is not what I need. The output should have 336 row. I want this:
For example:
The first row of output be:
(row1 from first cell arrays + row1 from second cell array + row1 from third cell array)/3
the second row of output be:
(row2 from first cell arrays + row2 from second cell array + row2 from third cell array)/3
the third row of output:
(row3 from first cell arrays + row3 from second cell array + row3 from third cell array)/3
.
.
.
For the last row of output (336th)
(row336 from first cell arrays + row336 from second cell array + row336 from third cell array)/3
Since this cell arrays each one represents for certain latitude and longitude I can't make a table from them.
Thank you so much
You can just as easily make a table out of them as not....just keep a corollary variable of the condition.
If you can average across them, then you can certainly combine them in some fashion to be able to do so far more easily than this.
There's not enough information on the specifics to be able to specify what the better storage model would be, but certainly this isn't it.
Thank you so much for let me know. ???

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2020a

Asked:

BN
on 16 Apr 2020

Commented:

BN
on 17 Apr 2020

Community Treasure Hunt

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

Start Hunting!