Calculating daily Average if only, the value is not zero
3 次查看(过去 30 天)
显示 更早的评论
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
采纳的回答
Guillaume
2016-10-17
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');
%...
更多回答(2 个)
KSSV
2016-10-17
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
2016-10-17
编辑:Andrei Bobrov
2016-10-17
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)];
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!