Accumarray with tall arrays

6 ビュー (過去 30 日間)
Greg
Greg 2017 年 3 月 15 日
コメント済み: Greg 2017 年 3 月 16 日
Accumarray is not in the list of functions that support tall arrays. Is there a way to do what it does with tall arrays?
For example, suppose I have a tall array of dates tt.DATE and a tall array of corresponding values tt.VAL. How can I sum tt.VAL for each unique date in tt.DATE?
uniqDate = gather(unique(tt.DATE);
sumVal = zeros(length(uniqDate),1);
for i = 1:length(uniqDate)
thisInd = tt.DATE == uniqDate(i);
thisSum = gather(tt.VAL(thisInd));
sumVal(i, 1) = thisSum;
end
This approach works except that it requires a call to gather at each step so it is far too slow. If I could write the gather statement outside of the loop somehow, I imagine that would help, but I can't figure out how to do it.

採用された回答

Edric Ellis
Edric Ellis 2017 年 3 月 16 日
You can use findgroups and splitapply to do this, like so:
% Make some example data
tt = tall(table(datetime(2017, 03, randi([1 31], 100, 1)), ...
rand(100, 1), 'VariableNames', {'Date', 'Value'}));
% group by date
g = findgroups(tt.Date);
% Call splitapply to find the sum of values on each unique date
sumAndDate = splitapply(@(v, d) {sum(v), d(1)}, tt.Value, tt.Date, g)
Note that because findgroups for tall arrays doesn't support the second output argument, I've concocted a slightly unusual function for the splitapply stage which returns both the sum and the datetime corresponding to that group.
  1 件のコメント
Greg
Greg 2017 年 3 月 16 日
This seems to work quite well and also generalizes easily to more dimensions. Thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by