How can I nest two loops of different dimension without using a for loop?

Hi,
As the title says, I'm trying to nest two loops without using for loops, since they really delay the computation time.
For example:
I want n to vary from 1 to 365 (for each day of the year). Within that loop, I want h to vary from 11 to -12 (24 values for 24 hours) for each day.
n=1:365
delta=sind(360.*n)
h=11:-1:-12
i=1:length(h); %For indexing, because I can't index negative values
theta=cosd(delta).*sind(15.*h)
a=cosd(theta)
b=cosd(phi)./2
c=cosd(phi)./2
z(i)=a+b+c
%Increase n, repeat loop and add result to z each time
%Plot sum of loop return
figure(1)
plot(z)
I hope I was clear with my question. I would like the calculations done for one hour, put into z, done for the second hour, added to z, etc etc for the whole day (24 hours). Then, once the day completed, I want n to increase and redo the 24 hours. I want n to finish after 365 days.
I appreciate your time and input.

댓글 수: 2

Amit
Amit 2014년 1월 31일
편집: Amit 2014년 1월 31일
where is n or h coming in calculation here?
Sorry, I was trying to simplify all my equations. I will edit the original code to make it more clear.

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

 채택된 답변

Amit
Amit 2014년 1월 31일
편집: Amit 2014년 1월 31일
phi = 0.1; % Undefined
n = 1:365;
delta = sind(360*n);
h = 11:-1:-12;
a= cos(bsxfun(@times,cosd(delta),sind(15*h)'));
b=cosd(phi)./2;
c=cosd(phi)./2;
z = a+b+c; % in z: 24 x 365 matrix

댓글 수: 5

Great answer, can you elaborate a little on it though?
Basically, this will set n=1, calculate delta, calculate A for the 24 values of h WHILE n is still equal to 1, calculate b,c,z and redo the loop for n=2 etc etc, right?
If that is correct, can you go elaborate a little about "bsxfun", and "@times"?
Thanks for your reply.
no. You do not need loops.
The code I wrote here, calculates within that code for all 365 days and 24 hours.
bsxfun is a element-by-element operation. More details can be found at: http://www.mathworks.com/help/matlab/ref/bsxfun.html
I do understand that bsxfun can be confusing. Another approach instead of bsxfun is using repmat, which is simply repeating the vector given number of times. Using repmat, the line a will change like this:
a= cos(repmat(cosd(delta),24,1).*repmat(sind(15*h)',1,365));
Sorry, I shouldn't have used the word "loop" in there.
What I meant to ask, is the following:
I wanted to confirm that this code sets n=1, computes the calculations when h varies over the 24 values WHILE n still equals 1, right? And does this for each n?
That is correct.
Z has 24 rows and 365 columns. Lets pick column 1 in z, then rows 1 to 24 in column represents values for z corresponding to n = 1 and h from 11 to -12 (all 24 values)
Excellent and informative replies. Thank you for your time. It is much appreciated.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2014년 1월 31일

댓글:

2014년 1월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by