필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
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
Maitiumc
Maitiumc 2017년 3월 22일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2017년 3월 22일
편집: Stephen23 2017년 3월 22일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by