How to subtract specific index for every year

조회 수: 1 (최근 30일)
soe thiha
soe thiha 2021년 3월 23일
댓글: soe thiha 2021년 3월 24일
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.

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 24일
편집: Walter Roberson 2021년 3월 24일
summer_idx = ismember(data_month, 3:5);
rainy_idx = ismember(data_month, 6:10);
winter_idx = ismember(data_month, [11:12, 1:2]);
  댓글 수: 1
soe thiha
soe thiha 2021년 3월 24일
Hi Walter
Thank you very much for your help.
This is a beautiful code.
Regards,

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by