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