Effacer les filtres
Effacer les filtres

Adding specific elements in a vector

1 vue (au cours des 30 derniers jours)
Queena Edwards
Queena Edwards le 8 Mar 2022
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

Réponse acceptée

Matt J
Matt J le 8 Mar 2022
Modifié(e) : Matt J le 8 Mar 2022
e=1:max(a(:,1))+1;
e([2,22:24,26:38])=[];
G=discretize(a(:,1),e);
result=accumarray(G,a(:,2))
  1 commentaire
Queena Edwards
Queena Edwards le 8 Mar 2022
Thank you. This works!!!

Connectez-vous pour commenter.

Plus de réponses (1)

Cris LaPierre
Cris LaPierre le 8 Mar 2022
I would use groupsummary with defined bin edges of [1,3:21,25,38].
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)
aGrp = 21×2 table
BG B ________ ______ [1, 3) 207.98 [3, 4) 85.32 [4, 5) 139.13 [5, 6) 127.19 [6, 7) 70.54 [7, 8) 76.89 [8, 9) 113.17 [9, 10) 52.8 [10, 11) 29.63 [11, 12) 23.53 [12, 13) 99.73 [13, 14) 46.62 [14, 15) 78.93 [15, 16) 68.8 [16, 17) 35.74 [17, 18) 83.02
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'})
A = 30×2 table
Duration Amount ________ ______ 1 71.87 2 136.11 3 85.32 4 139.13 5 127.19 6 70.54 7 76.89 8 113.17 9 52.8 10 29.63 11 23.53 12 99.73 13 46.62 14 78.93 15 68.8 16 35.74
AGrp = groupsummary(A,'Duration',[1,3:21,25,38],'sum','Amount')
AGrp = 21×3 table
disc_Duration GroupCount sum_Amount _____________ __________ __________ [1, 3) 2 207.98 [3, 4) 1 85.32 [4, 5) 1 139.13 [5, 6) 1 127.19 [6, 7) 1 70.54 [7, 8) 1 76.89 [8, 9) 1 113.17 [9, 10) 1 52.8 [10, 11) 1 29.63 [11, 12) 1 23.53 [12, 13) 1 99.73 [13, 14) 1 46.62 [14, 15) 1 78.93 [15, 16) 1 68.8 [16, 17) 1 35.74 [17, 18) 1 83.02
  1 commentaire
Queena Edwards
Queena Edwards le 8 Mar 2022
Thank you. This works great as well!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by