Reputation: 45
I am a beginner at using MATLAB. I have data which has 17 columns showing wave heights and wind data over a period of 7 years. First 4 columns are the years (2005-2011), month, day and hour respectively. And the next columns are the respective recordings.
I found a useful script here to plot these columns.
fid = fopen('wam1.txt','r');
C = textscan(fid,'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f','headerlines',4,'commentstyle','--');
fclose(fid);
x = C{1}; y1 = C{5}; y2 = C{3};
plot(x,y1)
Now, the graph consists of (for example) wind speed on Y axis and year (2005-2011) on X axis. And I have to find the average of wind speed for each year. Could anyone help me please.
Thanks.
Upvotes: 0
Views: 1114
Reputation: 21563
As you appear to plot the values of y1
the solution would be very simple.
mean(y1)
Will give you the average of y1.
If you want to do this by year, this will let you loop through the years and store the mean in a vector called result
.
count = 0;
for t = unique(x)
count = count+1;
result(count) = mean(y1(x==t)); % Or just result(t)
end
result
Upvotes: 1
Reputation: 7817
If there are the same number of values for each year, and they are in order (e.g. all values for 2005 first, then all values for 2006...) you could use reshape:
y1 = reshape(y1,365,7); mean(y1)
Otherwise, you can calculate for each year:
mean(y1(x==2005))
Upvotes: 1