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
  1 件のコメント
Alex Mcaulley
Alex Mcaulley 2019 年 5 月 13 日
Do you mean this?

サインインしてコメントする。

採用された回答

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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by