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

10 Ansichten (letzte 30 Tage)
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 Kommentar
Maitiumc
Maitiumc am 22 Mär. 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 22 Mär. 2017
Bearbeitet: Stephen23 am 22 Mär. 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

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by