Several loops in Matlab

1 Ansicht (letzte 30 Tage)
m m
m m am 13 Mai 2019
Kommentiert: Jan am 16 Mai 2019
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

Akzeptierte Antwort

Jan
Jan am 13 Mai 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 Kommentare
Jan
Jan am 13 Mai 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
Jan am 16 Mai 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;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements 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!

Translated by