Getting the average of a vector
23 次查看(过去 30 天)
显示 更早的评论
Hello, I want to find the average daily precipitation over a year. I am given the rainfall recorded every 30 mins i.e. there are 48 readings per day. I am trying to find the average per day. However, the code I am using does not take into account the zero readings i.e. it does not include the times when there was zero precipitation over a 30 minute period.This results in the average daily rain being overestimated
Additionally, if the rainfall is zero over a particular day, i want this to be represented by a zero in the output vector - Matlab tends to ignore zero values it seems.
Here is my code
e = xlsread ('KerryRainGaugedata2010.xls',1);
%Find average daily precipitation
for i = 1:48:17520
%f is precipitation
f = e(i:i+47, 8);
%g is mean daily precipitation
g(i) = (sum(f)./sum(f~=0));
end
g(g==0) = [];
Can anybody help?
Thanks :)
0 个评论
回答(2 个)
Wayne King
2011-11-26
Hi Maeve, how about just reshaping the data so you have 48 rows by 365 columns, then just taking the mean of the columns?
x = randn(17520,1);
x = reshape(x,48,365);
% column is one day
xavg = mean(x);
plot(xavg); xlabel('Days');
2 个评论
Wayne King
2011-11-27
Maeve, I did not mean for you to use randn(). I was just giving you an example. Because I do not have your data, I cannot do an example with your data. However, if you read your data into MATLAB as a 17520x1 vector, then you can use the above. If it reads in as 1x17520, then just do
x = x';
and you can still use the above.
Image Analyst
2011-11-26
Is it a problem with your "zeros" actually being blank cells in your workbook. No rain was recorded for those dates so it just left the cell empty. If so, then you might have to get all three outputs of xlsread and handle the case where the cells in the raw return arg are empty using isempty().
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!