Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
Maitiumc
Maitiumc le 22 Mar 2017
Modifié(e) : Stephen23 le 22 Mar 2017
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 commentaire
Maitiumc
Maitiumc le 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

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 22 Mar 2017
Modifié(e) : Stephen23 le 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

Plus de réponses (0)

Catégories

En savoir plus sur Matrices and Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by