calculate the annual discharge over several years

4 ビュー (過去 30 日間)
Maria Mateus
Maria Mateus 2013 年 10 月 29 日
コメント済み: Maria Mateus 2013 年 10 月 29 日
Hello,
I have a time series dataset that consists of daily discharge (Q) data from 1915-2006. My matrix: the first column is the year, the second is the month, the third is the day and the fourth column is the data of interest (Q). I need to calculate the mean annual discharge for each year and then arrange the data from the smallest Q to largest Q but knowing to which year that Q belongs.
  • Year Month day Q
  • 1915 10 1 2.13E+02
  • 1915 10 2 2.89E+02
  • 1915 10 3 8.10E+02
  • ...
  • ...
  • 2006 12 31 2.20E+04
I am very new at Matlab and even though I found some similar examples online I can't figure this out. Any help will be really appreciated.
Thanks!
  2 件のコメント
sixwwwwww
sixwwwwww 2013 年 10 月 29 日
編集済み: sixwwwwww 2013 年 10 月 29 日
Can you show sample for your data? Perhaps you can also look at:
doc mean
in your MATLAB command window and it will give you some idea
Jeremy
Jeremy 2013 年 10 月 29 日
Also look at the 'sort" function, specifically when called with 2 outputs.

サインインしてコメントする。

採用された回答

Cedric
Cedric 2013 年 10 月 29 日
編集済み: Cedric 2013 年 10 月 29 日
EDIT after you edited the question: it seems that there are 4 columns and not 2, and the discharge is in column4. I am updating my answer accordingly.
Here is an example
data = [1915, 7, 1, 6; ...
1915, 10, 3, 8; ...
1915, 12, 8, 7; ...
1916, 4, 22, 3; ...
1916, 7, 13, 2] ;
% - Get unique years and assign IDs starting at 1.
years = unique( data(:,1) ) ;
yearId = data(:,1) - min( data(:,1) ) + 1 ;
% - Compute Q mean per year.
Qmean = accumarray( yearId, data(:,4), [], @mean ) ;
% - Sort Qmean and get corresponding years.
[Qmean_sorted, ix] = sort( Qmean ) ;
years_sorted = years(ix) ;
Running this, you get
>> Qmean
Qmean =
7.0000
2.5000
>> Qmean_sorted
Qmean_sorted =
2.5000
7.0000
>> years_sorted
years_sorted =
1916
1915
  2 件のコメント
Maria Mateus
Maria Mateus 2013 年 10 月 29 日
Thank you so much. This works great!
Cedric
Cedric 2013 年 10 月 29 日
You're welcome.

サインインしてコメントする。

その他の回答 (1 件)

sixwwwwww
sixwwwwww 2013 年 10 月 29 日
Dear Maria, here is the code for required functionality:
ID = fopen('filename.txt');
data = textscan(ID, '%f%f%f%f');
fclose(ID);
year = data{1};
interest = data{4};
count = 1;
i = 1915:2006;
avg = zeros(length(i), 2);
for i = 1915:2006
ind = find(ismember(year, i));
avg(count, 1) = i;
avg(count, 2) = sum(interest(ind)) / length(ind);
count = count + 1;
end
avg = sortrows(avg, 2);
disp(avg)
I hope it helps. Good luck!
  1 件のコメント
Maria Mateus
Maria Mateus 2013 年 10 月 29 日
Thank you. This was also really helpful. :)

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by