How do i get matlab to break from a nested loop but then go back into the loop?
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Robert
am 22 Feb. 2016
Kommentiert: Robert
am 22 Feb. 2016
So ill try to be as clear as i can
Im basically trying to do a double summation here.
The problem is there are two variables that must advance at the same time.
When i use break it kicks me out of my inner loop goes outside, continues the outer one but never gets back to the inner one?
What do i do?
count = 0
sum = 0
for N = [-5,-4,-3,-2,-1,0,1,2,3]
for X = [3,1,-5,-11,0,-5,3,3,8]
sum = sum + X*exp(-j*pi*N)
%count was used just for debugging to see things line by line
count = count + 1
break
end
end
disp(sum);
So as you can see from my code for the first run through N = -5 It should go to the next loop where X = 3 and make a calculation. then it should break out of that loop and return to the top. Index N up so it now = -4 and then go back to my inner for loop where X should = 1 and continue calculation.
However whoever i try to use break it never goes back to the inner loop and just keeps advancing the outer loop without doing anything.
Also can someone tell me how to set up matlab to debug line by line like a C code
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 22 Feb. 2016
Nvals = [-5,-4,-3,-2,-1,0,1,2,3];
XVals = [3,1,-5,-11,0,-5,3,3,8];
total = 0;
for idx = 1 : length(Nvals)
N = Nvals(idx)
X = Xvals(idx)
total = total + X*exp(-j*pi*N)
end
Or more simply
N = [-5,-4,-3,-2,-1,0,1,2,3];
X = [3,1,-5,-11,0,-5,3,3,8];
total = sum(X .* exp(-j*pi*N));
3 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!