# How to get multiple numbers in a row for an Array

6 views (last 30 days)
RTG on 2 Mar 2020
Answered: Reshma Nerella on 6 Mar 2020
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 Comment

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);

Reshma Nerella on 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

R2019b

### Community Treasure Hunt

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

Start Hunting!