Effacer les filtres
Effacer les filtres

How to get multiple numbers in a row for an Array

1 vue (au cours des 30 derniers jours)
RTG
RTG le 2 Mar 2020
Modifié(e) : Jayla Wesley le 11 Fév 2022
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 commentaire
TADA
TADA le 2 Mar 2020
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);

Connectez-vous pour commenter.

Réponses (1)

Reshma Nerella
Reshma Nerella le 6 Mar 2020
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 commentaire
Jayla Wesley
Jayla Wesley le 11 Fév 2022
Modifié(e) : Jayla Wesley le 11 Fév 2022
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?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by