Several loops in Matlab
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
Can matlab calculate only the first iteration (i=1) of the loop1 and get out of the loop to compute the first iteration for the secod loop?
is there a way to do that?
example:
for i=1:N
A(i)=a*B(i)
end
for i=N:-1:1
B(i)=......
end
Risposta accettata
Jan
il 13 Mag 2019
A break can stop a loop prematurely:
for i = 1:N
A(i) = a*B(i);
break;
end
for i = N:-1:1
B(i) = rand;
end
But this seems to be to indirect. If you want to run the first iteration of the first loop only, write this explicitely:
A(1) = a = B(1);
Do not create a loop, if you do not want to run it.
Which is the actual problem you want to solve?
3 Commenti
Jan
il 13 Mag 2019
This would be extrem confusing. It sounds like you want to run both commands in one loop:
for i = 1:N
A(i) = a*B(i)
j = N - i + 1;
B(k) = rand;
end
Jan
il 16 Mag 2019
Sorry, I'm unable to read this code because the pile of lowercase characters disturb my eyes. I do not understand the mutual dependencies and the dynamic indentation scheme is tedious also.
Why do you waste time and energy with writing:
E=V(i+1)-V(i);
A=V(i+1)-V(i)/2;
B=V(i)-V(i-1)/2;
je=(ne(i+1) - ne(k,i).*exp(Te1).*Te1)./((dx);
if the values are not used anywhere? What is "k" here? And in addition, this code will not even run due to a missing trailing parenthesis.
Please post clean and clear running code. Remove unused code sections and press Ctrl-a Ctrl-i to get a proper indentation.
Omit such code inside a loop:
ap(i)= 1;
bp(i)= -2;
cp(i)= 1;
dp(i)= -(ni(i)-ne(i));
if you can do this easily outside:
ap = ones(1, nx - 1);
bp = repmat(-2, 1, nx - 1);
cp = ones(1, nx - 1);
dp = ne - ni;
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Loops and Conditional Statements in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!