How to subtract specific index for every year

2 visualizzazioni (ultimi 30 giorni)
Dear Senior Matlab Users
I have data with 2 columns and 72 rows (72x2) as shown in Figure. First column is month and Second column is data.
I want to extract and sum the seasonal data from several years.
There are three season (1) Summer (2) Rainy (3) Winter. Different colors are used to hightlight the seasons.
Summer= March;April;May; (3,4,5)
Rainy= June to October; (6;7;8;9;10)
Winter=January;Feburary;November;December (11;12;1;2)
I tried some ugly code for first two years like this.
clear all;
data=xlsread('month.xlsx');
TVH_1=data(1:end,2);
summer_idx=[3 4 5 15 16 17 27 28 29 39 40 41 51 52 53 63 64 65]';
winter_idx=[1 2 11 12 13 14 23 24 25 26 35 36 37 38 47 48 49 50 59 60 61 62 71 72];
rainy_idx=[6 7 8 9 10 18 19 20 21 22 30 31 32 33 34 42 43 44 45 46 54 55 56 57 58 66 67 68 69 70];
TVH_1_summer=sum(TVH_1(summer_idx));
TVH_1_winter=sum(TVH_1(winter_idx));
TVH_1_rainy=sum(TVH_1(rainy_idx));
TVH_1_total=(TVH_1_summer + TVH_1_winter + TVH_1_rainy);
TVH_1= table(TVH_1_summer, TVH_1_winter, TVH_1_rainy, TVH_1_total)
According to this code, I have to find the seasonal month index manually which can lead selecting wrong month for long run.
So, can someone kindly teach me how to extract specific month neat and tidly using for loop or while loop, please?
Thank you very much in advance.

Risposta accettata

Walter Roberson
Walter Roberson il 24 Mar 2021
Modificato: Walter Roberson il 24 Mar 2021
summer_idx = ismember(data_month, 3:5);
rainy_idx = ismember(data_month, 6:10);
winter_idx = ismember(data_month, [11:12, 1:2]);
  1 Commento
soe thiha
soe thiha il 24 Mar 2021
Hi Walter
Thank you very much for your help.
This is a beautiful code.
Regards,

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by