Calculating daily Average if only, the value is not zero

3 views (last 30 days)
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

Accepted Answer

Guillaume
Guillaume on 17 Oct 2016
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');
%...

More Answers (2)

KSSV
KSSV on 17 Oct 2016
YOu can pick only non-zero numbers alone and get the average. How about the below code? I have generated some random data and introduced zeros randomly.
clc; clear all ;
data = rand(8760,1) ; % random data
data(randsample(1:8760,8760/2))= 0; % introduce zeros randomly
data = reshape(data,24,[]); % reshape
% calculate avarage for first daya
data_avg = zeros(1,size(data,2)) ;
for i = 1:size(data,2)
idx = (data(:,1)~=0) ; % get non-zero elements
data_avg(i) = sum(data(:,1))/sum(idx) ;
end

Andrei Bobrov
Andrei Bobrov on 17 Oct 2016
Edited: Andrei Bobrov on 17 Oct 2016
let data - your matrix (8760 x 2), here first column - timestamp (serial date number), second - solar radiation.
t = data(:,2) > 0;
D = datevec(data(t,1));
[a,~,c] = unique(D(:,1:3),'rows');
out = [a, accumarray(c,data(t,2),[],@mean)];

Categories

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