Adding specific elements in a vector
1 次查看(过去 30 天)
显示 更早的评论
I have the following vector "a", where the values in the first column are the duration of rainfall and the values in the second column are the amount of rainfall.
a =
1.0000 71.8700
2.0000 136.1100
3.0000 85.3200
4.0000 139.1300
5.0000 127.1900
6.0000 70.5400
7.0000 76.8900
8.0000 113.1700
9.0000 52.8000
10.0000 29.6300
11.0000 23.5300
12.0000 99.7300
13.0000 46.6200
14.0000 78.9300
15.0000 68.8000
16.0000 35.7400
17.0000 83.0200
18.0000 105.4000
19.0000 15.7200
20.0000 13.6600
21.0000 48.9600
22.0000 43.8700
23.0000 6.8100
24.0000 39.3100
25.0000 22.5900
27.0000 77.1500
29.0000 45.1900
30.0000 17.2500
34.0000 11.1600
38.0000 32.1900
I would like to sum the amount of rainfall for the duration 1-2h, 21-24h and 25-38h. The results should look like:
a =
1-2 207.9800
3.0000 85.3200
4.0000 139.1300
5.0000 127.1900
6.0000 70.5400
7.0000 76.8900
8.0000 113.1700
9.0000 52.8000
10.0000 29.6300
11.0000 23.5300
12.0000 99.7300
13.0000 46.6200
14.0000 78.9300
15.0000 68.8000
16.0000 35.7400
17.0000 83.0200
18.0000 105.4000
19.0000 15.7200
20.0000 13.6600
21-24 138.9500
25-38 205.5300
0 个评论
采纳的回答
更多回答(1 个)
Cris LaPierre
2022-3-8
a = [1.0000 71.8700
2.0000 136.1100
3.0000 85.3200
4.0000 139.1300
5.0000 127.1900
6.0000 70.5400
7.0000 76.8900
8.0000 113.1700
9.0000 52.8000
10.0000 29.6300
11.0000 23.5300
12.0000 99.7300
13.0000 46.6200
14.0000 78.9300
15.0000 68.8000
16.0000 35.7400
17.0000 83.0200
18.0000 105.4000
19.0000 15.7200
20.0000 13.6600
21.0000 48.9600
22.0000 43.8700
23.0000 6.8100
24.0000 39.3100
25.0000 22.5900
27.0000 77.1500
29.0000 45.1900
30.0000 17.2500
34.0000 11.1600
38.0000 32.1900];
[B,BG] = groupsummary(a(:,2),a(:,1),[1,3:21,25,38],'sum');
aGrp = table(BG,B)
I prefer working with tables, so I would actually do it this way. You may find the Access Data in Tables page helpful.
A = array2table(a,'VariableNames',{'Duration','Amount'})
AGrp = groupsummary(A,'Duration',[1,3:21,25,38],'sum','Amount')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!