Calculate weekly mean from temperature data

1 view (last 30 days)
Hi!
Can anyone help me to calculate weekly means of temperature data (td= SST - bottom temp difference) and salinity data? Time is from 1st of July till 31st of August 2022 (hour 12:00:00 for every day).
Could not attach the data as mat files they are too big. My variables include:
td(temperature difference) =783x1230x62 double
sal=4-D double
time = 62x1 datetime
My friend used this code for the same procedure, but I need to change the number of weeks as it's different from my data (she had 13 weeks). I also don't know how to interpret what she did in the calculations ( (w-1)*7+1:w*7) ) so don't know how to apply it to my data. Thanks in advance!
for w = 1:13
tdw(:,:,w) = mean(td(:,:,(w-1)*7+1:w*7),3);
salw(:,:,:,w) = mean(sal(:,:,:,(w-1)*7+1:w*7),4);
end

Accepted Answer

Antoni Garcia-Herreros
Antoni Garcia-Herreros on 31 Mar 2023
Edited: Antoni Garcia-Herreros on 4 Apr 2023
Hello Giulia,
You may try something like this.
If you need more info on conditional statements, you can refer to this page
td=rand(783,1230,62); %Generate variables for the example
sal=rand(783,1230,3,62);
l=1:7:size(td,3); % Indices to separate the days in the week e.g. 1 8 15 ...
tdw=zeros([size(td,[1 2]) size(l,2)]); % Array where the means of td for every week will be stored
salw=zeros([size(sal,[1 2 3]) size(l,2)]); %Same but for sal
for i=1:numel(l) %Loop through the first day of the weeks 1 8 15 ...
if i+1>numel(l) % This will only be true for the last week. Because if we call l(i+1) it will give us an error
% And because the last week is not 7 days, we take the mean of td
% and sal from day l(end), 57, to end of td, 62
tdw(:,:,i)=mean(td(:,:,l(i):end),3);
salw(:,:,:,i) = mean(sal(:,:,:,l(i):end),4);
else % The rest of the iterations will enter here, where we take the mean between of td or sal between the first and last day of week i
tdw(:,:,i)=mean(td(:,:,l(i):l(i+1)-1),3);
salw(:,:,:,i) = mean(sal(:,:,:,l(i):l(i+1)-1),4);
end
end
Hope this helps!
  3 Comments
Giulia
Giulia on 4 Apr 2023
Hi again Antoni, just as a clarification as I am quite new to Matlab...what is the 'if ... else ... end' statement used for? Thanks!
Giulia
Giulia on 4 Apr 2023
Thanks again for the clarification!
Best

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB 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!