How to subtract specific index for every year

2 Ansichten (letzte 30 Tage)
soe thiha
soe thiha am 23 Mär. 2021
Kommentiert: soe thiha am 24 Mär. 2021
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 24 Mär. 2021
Bearbeitet: Walter Roberson am 24 Mär. 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 Kommentar
soe thiha
soe thiha am 24 Mär. 2021
Hi Walter
Thank you very much for your help.
This is a beautiful code.
Regards,

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by