Help using accumarray on a 3d-matrix

6 ビュー (過去 30 日間)
lightworks
lightworks 2015 年 10 月 26 日
コメント済み: lightworks 2015 年 11 月 1 日
Hello,
I have a 3-d data array of rainfall over an area named PP. The dimensions are (longitud, latitude, time):
PP < X x Y x T double >
So for each time I have a 2-d array which contains the rainfall values for every point in my (x,y). For example, time = 1 (day 1) contains something like this:
PP(:,:,1) =
0.0 2.2 1.0
4.1 5.0 4.5
2.0 1.9 1.5
...
and so on. The third column of PP (the time) is the index of each day of my period, which corresponds to the row number in an array called date and looks like this:
date < T x 3 double>
date(:,:)=
1999 01 01
1999 01 02
1999 01 03
...
I need the average of the rainfall data over every year (year 1999 mean, year 2000 mean, an so on). I also need the average of all the years during the summer months.
I tried to use accumarray to do the average rainfall over every year. But as elegant/efficient as this function might be, it's hard to understand how it actually works, specially for newbies like me. This is what I tried:
for t=1:size(data,3);
subtot(:,:,t)=accumarray(date(:,1),data(:,:,t), [], @mean);
end
which resulted in:
Error using accumarray
Second input VAL must be a vector with one element for each row in SUBS, or a scalar.
The Matlab documentation is not enough for me to understand how to use the accumarray in this case. Also, I don't understand how to use it when I want to take the average of every summer (Summer 1999, summer 2000, summer 2001, ...)
Am I going in the right direction? If so, how can I properly use the function so it does what I want?
Any help/advice is more than welcome. Thank you for taking your time in advance, and sorry for my poor English.

採用された回答

David Young
David Young 2015 年 10 月 27 日
I think it's simpler to use mean directly and avoid accumarray. So to get all the rainfall for 1999, for example, you could do something like this:
days1999 = date(:,1) == 1999;
pp1999 = pp(:,:,days1999);
and then to get the mean for each location in 1999:
mean1999 = mean(pp1999, 3); % 3 means average over time
or to get the overall mean for 1999:
overallMean1999 = mean(pp1999(:));
To select summer months (say June, July, August)
daysSummer = date(:,2) >= 6 & date(:,2) <= 8;
ppSummer = pp(:,:,daysSummer);
etc.
If you are doing several years you could replace the value 1999 by a variable and carry out the computation in a loop.
  1 件のコメント
lightworks
lightworks 2015 年 11 月 1 日
Perfect! Thank you so much :D

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by