Info

This question is closed. Reopen it to edit or answer.

i have trouble making this into a for loop.

1 view (last 30 days)
zunhern chong
zunhern chong on 1 Nov 2017
Closed: MATLAB Answer Bot on 20 Aug 2021
JAN = sum(Pmax(1:744)) ;
FEB = sum(Pmax(745:1416));
MAR = sum(Pmax(1417:2160));
APR = sum(Pmax(2162:2880));
MAY = sum(Pmax(2881:3624));
JUN = sum(Pmax(3625:4344));
JUL = sum(Pmax(4345:5088));
AUG = sum(Pmax(5089:5832));
STEP = sum(Pmax(5833:6552));
OCT = sum(Pmax(6553:7296));
NOV = sum(Pmax(7297:8016));
DEC = sum(Pmax(8017:8760));
E_MONTH = vertcat(JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,STEP,OCT,NOV,DEC);
  2 Comments
Stephen23
Stephen23 on 1 Nov 2017
Edited: Stephen23 on 1 Nov 2017
"i have trouble making this into a for loop."
Well, that is not a surprise: you created lots of separate variables. Creating lots of separate variables does make processing them in a loop hard (slow, complex, buggy, hard to debug...)
Solution: don't create lots of individual variables. After all this is MATLAB, whose names comes from "MATrix LABoratory", and not from "Lets split up our data into lots of separate variables and make our code pointlessly complex".
Use indexing, which is simple and very efficient. Or tables. Or fieldnames. Or any other standard way of splitting data efficiently.
In fact it looks like you are trying to group data by month: using tables is likely the best option, and you will find many useful tools that allow you to do things like calculate the sum, means, etc, for each month, without requiring lots of separate variables.
zunhern chong
zunhern chong on 1 Nov 2017
Hi, do you know how to group that by month using inxed ?

Answers (1)

KL
KL on 1 Nov 2017
you don't need for loop. Create a datetime vector and do it the proper way.
dt = datetime([2017 01 01 00 00 00]):hours(1):datetime([2017 12 31 23 00 00]);
timeTable = timetable(dt,Pmax);
monthlyMean = retime(timeTable,'monthly','mean')
  2 Comments
KL
KL on 1 Nov 2017
Which version are you using? if 2016b or later, why not use this method?

This question is closed.

Tags

Community Treasure Hunt

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

Start Hunting!