Getting the average of a vector

37 views (last 30 days)
Maeve  Ryan
Maeve Ryan on 26 Nov 2011
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 :)

Answers (2)

Wayne King
Wayne King on 26 Nov 2011
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 Comments
Maeve  Ryan
Maeve Ryan on 26 Nov 2011
If I am not mistaken, randn produces an array of random numbers. I do not random numbers. I want to plot the data from the excel file. Any suggestions?
Wayne King
Wayne King on 27 Nov 2011
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.

Sign in to comment.


Image Analyst
Image Analyst on 26 Nov 2011
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().
  1 Comment
Maeve  Ryan
Maeve Ryan on 26 Nov 2011
No there are no blank cells in the excel file. It says "0" when there is no rainfall. I want to plot the daily rainfall for the 365 days..do you know how I could do this?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!