Several loops in Matlab

1 次查看(过去 30 天)
m m
m m 2019-5-13
评论: Jan 2019-5-16
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

采纳的回答

Jan
Jan 2019-5-13
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 个评论
Jan
Jan 2019-5-13
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 2019-5-16
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;

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by