Borrar filtros
Borrar filtros

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

4 visualizaciones (últimos 30 días)
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 comentario
Maitiumc
Maitiumc el 22 de Mzo. de 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

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 22 de Mzo. de 2017
Editada: Stephen23 el 22 de Mzo. de 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

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by