Finding mean value through every 12 columns of nested doubles in cell

Hi all,
I'm struggling to adapt previous documentation to my specific case. I have variables, "d" for example below, that are 1x36 cells comprised of nested doubles of the size 771x248-ish (changes size depending on days in a month). Each double is a month's worth of data, and I am trying to find the yearly average over the course of 3 years for one location, so only looking at the values in the first row, all columns (1,:) of the variable (will adapt this to a 30 year case).
The montly average gives me a 1x36 double, which is good.
For the yearly average, I want to obtain a 1x3 double, i.e. one average per year for three years. Again, I am only concerned with the first row, all columns of each nested double in the original cell. Below is what I think the script should look like, but I am only getting one value for the answer:
d_meana = [];
for kk = 1:12:36
d_meana = mean(d{1,kk}(1,:):d{1,kk+11}(1,:));
%will compute for other variables
end
I feel like this should be simple but I'm struggling as a newbie to Matlab. Any tips woudl be greatly appreciated, thank you in advance! Let me know if this is confusing, Itried to word it best I could.

 Accepted Answer

Gabrielle - d_meanm is a 1x36 array and you want to find the average of each of the three years, then you could reshape the array so that it is a 12x3 array where each column is a years worth of data. Then just find the average of that to get your 1x3 array
numberOfYears = 3;
d_meana = mean(reshape(d_meanm,12,numberOfYears));

8 Comments

Geoff, thank you for your help! This gives me a 1x3 array, which is great. My only question is now, is the output of this equal to the mean of the monthly means? If so, this doesn't seem like an accurate calculation, since ideally I need to take the mean of the entire original dataset, which is a 1x36 cell with nested doubles.
Gabrielle - I think that I misunderstood why you wanted to convert the 1x36 to a 1x3. What should the average of each of these three elements be?
I apologize for the confusion! I did not explain myself completely in my original question and will edit it to reflect what I meant. Thank you for bearing with me. Please see above the edited question!
Gabrielle - if the problem is that you are only getting one result instead of three, then try changing your code to the following
d_meana = [];
j = 1;
for kk = 1:12:36
d_meana(j) = mean(d{1,kk}(1,:):d{1,kk+11}(1,:));
j = j + 1;
%will compute for other variables
end
So for each iteration, we save the average to the jth element of d_meana so you should get a three element array.
That seems like it would work, and it gives me 3 values, but the first two are NaN.
I don't think d{1,kk}(1,:):d{1,kk+11}(1,:) is returning what we think it is returning. Can you try the following instead?
d_meana = [];
j = 1;
for kk = 1:12:36
d_meana(j) = mean(cell2mat(cellfun(@(x)x(1,:),d(kk:kk+11), 'UniformOutput', false)));
j = j + 1;
%will compute for other variables
end
The above code uses cellfun to process the 12 consecutive cell arrays (for years 1, 2, and 3) and extracts the first row from each. The result is a 1xM cell array (where M is some number of columns corresponding to the days of the months) which we then convert to a matrix with cell2mat which we can then find the mean of.
I think with the original code, only one value was being returned when we used
d{1,kk}(1,:):d{1,kk+11}(1,:))
and not all the elements in the first row from each year.
Geoff, this works great. Thank you for taking the time to help me, I truly appreciate it. I hope you have a nice rest of your day!

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

gd
on 5 Apr 2020

Commented:

on 6 Apr 2020

Community Treasure Hunt

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

Start Hunting!