accumarray does not return the correct sum
2 ビュー (過去 30 日間)
古いコメントを表示
Hello all,
I have a table dataset like below
table=
Date ID x y z Profit
'01-Jan-2019' 157350 0 1 100 0.470000000000000
'01-Jan-2019' 38715 0 1 100 1.05000000000000
'01-Jan-2019' 157350 0 1 100 0.540000000000000
'01-Jan-2019' 157350 0 1 100 0.320000000000000
'01-Jan-2019' 81074 1 1 100 2.28000000000000
'01-Jan-2019' 81074 1 1 100 1.66000000000000
'01-Jan-2019' 141788 0 1 100 0
'01-Jan-2019' 19105 0 1 300 -0.230000000000000
'01-Jan-2019' 19105 0 1 300 0.130000000000000
'01-Jan-2019' 5492 1 1 1500 7.51000000000000
'01-Jan-2019' 36435 0 1 100 0.110000000000000
'01-Jan-2019' 130445 1 1 100 -79.6700000000000
'01-Jan-2019' 130445 1 1 100 -78.4800000000000
'01-Jan-2019' 130445 1 1 100 -79.4300000000000
'01-Jan-2019' 154312 0 1 100 -2.39000000000000
'01-Jan-2019' 130445 1 1 100 -79.9200000000000
'01-Jan-2019' 130445 1 1 100 -80.7500000000000
'01-Jan-2019' 130445 1 1 100 -80.3100000000000
'01-Jan-2019' 130445 1 1 100 -80.0600000000000
'01-Jan-2019' 81074 1 1 100 1.62000000000000
I use
[ids, ~, rows] = unique(table(:, 2)); to get unique IDs and subscripts.
then I want to calculate the sum of positive profit and the sum of negative profit for each ID like:
myGrossprofit=accumarray(rows,table2array(table(:, 2)>0, @sum);
myGrossLoss=accumarray(rows, table2array(table(:, 2)<0, @sum);
But my results is not correct. It even shows an integer when Profit is decimal and my GrossLoss is a positive number.
Thanks in advance for any help.
5 件のコメント
Walter Roberson
2019 年 2 月 13 日
mask = YourTable{:,6}<0;
accumarray(rows(mask), YourTable{mask,6}, [max(rows),1],@sum)
回答 (1 件)
Stephen23
2019 年 2 月 13 日
編集済み: Stephen23
2019 年 2 月 13 日
Your data:
hdr = {'Date','ID','x','y','z','Profit'};
arr = {...
'01-Jan-2019' 157350 0 1 100 0.470000000000000
'01-Jan-2019' 38715 0 1 100 1.05000000000000
'01-Jan-2019' 157350 0 1 100 0.540000000000000
'01-Jan-2019' 157350 0 1 100 0.320000000000000
'01-Jan-2019' 81074 1 1 100 2.28000000000000
'01-Jan-2019' 81074 1 1 100 1.66000000000000
'01-Jan-2019' 141788 0 1 100 0
'01-Jan-2019' 19105 0 1 300 -0.230000000000000
'01-Jan-2019' 19105 0 1 300 0.130000000000000
'01-Jan-2019' 5492 1 1 1500 7.51000000000000
'01-Jan-2019' 36435 0 1 100 0.110000000000000
'01-Jan-2019' 130445 1 1 100 -79.6700000000000
'01-Jan-2019' 130445 1 1 100 -78.4800000000000
'01-Jan-2019' 130445 1 1 100 -79.4300000000000
'01-Jan-2019' 154312 0 1 100 -2.39000000000000
'01-Jan-2019' 130445 1 1 100 -79.9200000000000
'01-Jan-2019' 130445 1 1 100 -80.7500000000000
'01-Jan-2019' 130445 1 1 100 -80.3100000000000
'01-Jan-2019' 130445 1 1 100 -80.0600000000000
'01-Jan-2019' 81074 1 1 100 1.62000000000000
};
tbl = cell2table(arr,'VariableNames',hdr);
Code:
>> [G,ID] = findgroups(tbl.ID);
>> fun = @(v)[sum(v(v>0)),sum(v(v<0))];
>> Y = splitapply(fun,tbl.Profit,G);
>> [ID,Y]
ans =
5492 7.51 0
19105 0.13 -0.23
36435 0.11 0
38715 1.05 0
81074 5.56 0
130445 0 -558.62
141788 0 0
154312 0 -2.39
157350 1.33 0
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Preprocessing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!