How to use result in each iteration in a for loop and then use it in next loop?

조회 수: 5 (최근 30일)
m m
m m 2019년 6월 12일
답변: Prasanth Sikakollu 2019년 6월 12일
how can i use the result of the first iteration in other For loop?
  댓글 수: 3
m m
m m 2019년 6월 12일
i have two loops like this:
in the first loop i calculate x (for example) and in the second i calculate y, but in each iteration x depends on y.
for i=1:10
x=.....;
% here i will get the result fo i=1 but i must simultanously use it in the second loop
end
%second loop
for i=1:10
y=....;
end
Prasanth Sikakollu
Prasanth Sikakollu 2019년 6월 12일
You can merge both the loops and use the result in the same iteration
for i=1:10
x = 10*i; % This is the value generated
y = x + 1; % Using generated value x to evaluate y
end

댓글을 달려면 로그인하십시오.

답변 (2개)

KSSV
KSSV 2019년 6월 12일
for i = 1:10
a = rand+rand ; % this is the result in first loop
for j = 1:3
b = a+rand ; % result of first loop used in second loop
end
end

Prasanth Sikakollu
Prasanth Sikakollu 2019년 6월 12일
Merge both the loops and use the generated result in the same loop.
for i=1:10
x = 10*i; % This is the value generated. Expression on the RHS is a random one
y = x + 1; % Using generated value x to evaluate y
end
Hope this helps

카테고리

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