How to create an array which has an incremental value every 24 elements

11 views (last 30 days)
Say an array which has the incremental day of the year for every hour of the day, ie 24*365=8760 elements, of 365 different elements. So the first 24 would be 1, the next 24 would be 2 and so on.
I've tried this
aDays(8760,1) =NaN;
for n = 1:365
if rem(n/24,1)~=0
aDays(n,1)=n;
else
aDays(n,1)=n+1;
end
end
but this doesn't work and I'm guessing there is an easier way to do it.
  1 Comment
Maitiumc
Maitiumc on 22 Mar 2017
Between posting and coming back to check, I found that the following works. Obviously, the answer from Stephen is a better way, but thought I would post it anyway:
aDays(8760,1) =NaN;
i=1;
j=24;
m=1;
while m<366
aDays(i:j,1)=m;
i=i+24;
j=j+24;
m=m+1;
end

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 22 Mar 2017
Edited: Stephen23 on 22 Mar 2017
With MATLAB 2015A or newer use repelem:
vec = repelem(1:365,24)
Or for older versions of MATLAB this:
>> vec = reshape(repmat(1:365,24,1),[],1)
vec =
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
2
2
2
2
etc
>> size(vec)
ans =
8760 1

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!