MATLAB: Calculating daily Average if only, the value is not zero

dailyaverage

Hello,
I have a dataset, which I have been working on past few weeks.It has solar radiation hourly values(one column of 8760 values) and timestamp data (1 column of 8760 hourly values). I need to calculate the daily average of the solar radiation, which I was able to do using the reshape function.
However, that also took into account the solar radiation value "0 W/m2" during the night hours. I thus need to make a daily average for only solar hours.So looking at combining a for loop, with 'if' and finding averages. But I need help as I am a new coder. Thank you

Best Answer

If you already have an algorithm that compute the mean, the simplest thing would be to replace the 0 with NaN and add the 'omitnan' flag to your call to mean. Mathematically it also makes more sense to ignore nans when calculating mean rather than ignoring 0. So:
solarradiation(solarradiation == 0) = nan;
%...

%in your code calculating mean
something = mean(somesolarradiation, 'omitnan');
%...