years in fourth dimension is not printing properly

1 vue (au cours des 30 derniers jours)
Gurumoorthi K
Gurumoorthi K le 6 Juil 2023
Dear Matlab community,
I am trying to calculate monthly and seas (seasonal) mean/sum for each year at each grid. My output (mhwday_month1, mhwday_seas1,...) shows same values in all the year. Means, month 1 to 12 is ok, but it is printing same values for all the years (:, :, 1, 1 to :, :, 1, 2,.. etc). Can you suggest me what is missed? or please modify it. Thanks
date_used=datevec(datenum(1993,1,1):datenum(2016,12,31));
% Determining land index
land_index=isnan(nanmean(mhw_ts,3));
% Monthly
numYears = 24;
mhwday_month1=NaN(size(mhw_ts,1),size(mhw_ts,2),12,numYears); % lon-lat-month
mhwint_month1=NaN(size(mhw_ts,1),size(mhw_ts,2),12,numYears); % lon-lat-month
for i=1:12
index_used = date_used(:, 2) == i;
temp = sum(~isnan(mhw_ts(:, :, index_used)), 3, 'omitnan')./(2016-1993+1); % Calculate the average MHW days for each location and year
for j = 1:numYears
mhwday_month1(:, :, i, j) = temp; % Assign the values to the appropriate slice of mhwday_month1
mhwint_month1(:, :, i, j) = mean(mhw_ts(:, :, index_used), 3, 'omitnan');
end
end
mhwday_month1(repmat(land_index, 1, 1, 12, numYears)) = nan;
mhwint_month1(repmat(land_index, 1, 1, 12, numYears)) = nan;
% SPR-SON SUM-DJF AUT-MAM WIN-JJA
seas=[9 10 11;...
12 1 2;...
3 4 5;...
6 7 8];
mhwday_seas1 = NaN(size(mhw_ts, 1), size(mhw_ts, 2), 4, numYears);
mhwint_seas1 = NaN(size(mhw_ts, 1), size(mhw_ts, 2), 4, numYears);
for i = 1:4
index_used = ismember(date_used(:, 2), seas(i, :));
temp = sum(~isnan(mhw_ts(:, :, index_used)), 3, 'omitnan')./(3 * (2016-1993+1));
mhwday_seas1(:, :, i, :) = repmat(temp, [1, 1, 1, numYears]);
mhwint_seas1(:, :, i, :) = repmat(mean(mhw_ts(:, :, index_used), 3, 'omitnan'), [1, 1, 1, numYears]);
mhwday_seas1(repmat(land_index, 1, 1, 1, numYears)) = nan;
mhwint_seas1(repmat(land_index, 1, 1, 1, numYears)) = nan;
end
% Extracting monthly MHW days for all the year in a specific location
mhwday_month_domain = squeeze(mhwday_month1(:, :, :, :));
% Extracting MHW intensity for for all the year in a specific location
mhwint_month_domain = squeeze(mhwint_month1(:, :, :, :));
%mhwint_month_domain = reshape(mhwint_month_domain, 1, []);
Unrecognized function or variable 'mhw_ts'.

Réponses (2)

J
J le 6 Juil 2023
Hi Gurumoorthi,
From what I could understand from your question, I think the issue you're facing is caused by the way you're assigning values to mhwday_month1 and mhwint_month1 in the nested loop. You're assigning the same value temp to all years for each month. To fix this, you need to modify the assignment inside the nested loop. Here's the modified code:
for i = 1:12
index_used = date_used(:, 2) == i;
temp = sum(~isnan(mhw_ts(:, :, index_used)), 3, 'omitnan')./(2016-1993+1); % Calculate the average MHW days for each location and year
for j = 1:numYears
mhwday_month1(:, :, i, j) = temp(:, :, j); % Assign the values to the appropriate slice of mhwday_month1
mhwint_month1(:, :, i, j) = mean(mhw_ts(:, :, index_used), 3, 'omitnan');
end
end
Similarly, you need to make a similar modification in the nested loop for calculating seasonal means.
for i = 1:4
index_used = ismember(date_used(:, 2), seas(i, :));
temp = sum(~isnan(mhw_ts(:, :, index_used)), 3, 'omitnan')./(3 * (2016-1993+1));
for j = 1:numYears
mhwday_seas1(:, :, i, j) = temp(:, :, j);
mhwint_seas1(:, :, i, j) = mean(mhw_ts(:, :, index_used), 3, 'omitnan');
end
mhwday_seas1(repmat(land_index, 1, 1, 1, numYears)) = nan;
mhwint_seas1(repmat(land_index, 1, 1, 1, numYears)) = nan;
end
These modifications ensure that the values assigned to mhwday_month1 and mhwint_month1 are specific to each year within the nested loop. I believe this must solve your issue. Or else you could reply. Thanks
  1 commentaire
Gurumoorthi K
Gurumoorthi K le 6 Juil 2023
There is an issue with the code. It shows data in val(:,:,1,1). Remaining all month,years shows NaN. e.g. output is attached.
I am expecting :, :, month, year output. Then seasonal mean/sum (:, :, season, year). I thank and appretiate you for spending time for me.

Connectez-vous pour commenter.


Peter Perkins
Peter Perkins le 17 Juil 2023
I can't follow youre code, but my strong recommendation is to read in your data using readtimetable, and then use groupsummary or retime. It's one line of code to compute yearly or monthly summaries.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by