How to get multiple numbers in a row for an Array

7 visualizaciones (últimos 30 días)
RTG
RTG el 2 de Mzo. de 2020
Editada: Jayla Wesley el 11 de Feb. de 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 comentario
TADA
TADA el 2 de Mzo. de 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);

Iniciar sesión para comentar.

Respuestas (1)

Reshma Nerella
Reshma Nerella el 6 de Mzo. de 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 comentario
Jayla Wesley
Jayla Wesley el 11 de Feb. de 2022
Editada: Jayla Wesley el 11 de Feb. de 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?

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by