필터 지우기
필터 지우기

How to get multiple numbers in a row for an Array

조회 수: 1 (최근 30일)
RTG
RTG 2020년 3월 2일
편집: Jayla Wesley 2022년 2월 11일
Right now I am calculating the Equation of Time for a class. The EOT depends on the day of the year however for my code I need an answer to repeat itself 24 times before moving on to the next answer. For example if my first EOT function reads out 1, I want this 1 to continue 24 times and then move on to the next EOT function and so on. Eventually I want to have a matrix of 1 x 8760. Is there a way to do this? Here is what I have so far.
for n=1:1:365
for j =1:24
N(n) = (n-1)*(360/365);
EOT = 229.2*(.000075+0.001868*cos(N)- 0.032077*sin(N) - 0.014615*cos(2*N)- 0.04089*sin(2*N));
end
end
Any help is appreciated as I am stuck and my EOT Matrix is still at 1x365. Thank you.
  댓글 수: 1
TADA
TADA 2020년 3월 2일
you assign all of EOT each iteration, for the entire current value of N, which increases each iteration of the outer loop
If you want to get a 1x8760 you need to use indexing and set the values according to an index you will have to calculate using n and j
something like that:
EOT((n-1)*365 + j) = ...
you also don't use j (nor any random value) ever, so all 24 repeats will be identical
don't forget to preallocate your vectors for better performance:
EOT = zeros(1, 365*24);

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

답변 (1개)

Reshma Nerella
Reshma Nerella 2020년 3월 6일
Hi,
Since you are not finding any value varying with j in the inner for loop, you can remove it.
EOT = zeros(1, 365*24);
for n=1:365
N(n) = (n-1)*(360/365);
val = 229.2*(.000075+0.001868*cos(N)- 0.032077*sin(N) - 0.014615*cos(2*N)- 0.04089*sin(2*N));
EOT(1, (i-1)*24+1 : i*24) = val;
end
  댓글 수: 1
Jayla Wesley
Jayla Wesley 2022년 2월 11일
편집: Jayla Wesley 2022년 2월 11일
Code does not work, you receive the following error:
Index in position 2 is invalid. Array indices must be positive integers or logical values.
Can you provide a fix @Reshma Nerella?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by