How to get multiple numbers in a row for an Array

7 ビュー (過去 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 Center および File ExchangeLoops 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