필터 지우기
필터 지우기

calling an array which is initialized inside an if statement + for loop, outside the loop

조회 수: 1 (최근 30일)
hi, i have a if statement and it runs a for loop in each case where it is true and false. An array is initialized and assigned a value within this for loop (both cases). Outside the loops, I need both arrays to initialize another array. For example,
for i=1:v
if z==i
for j=1:m
A(j)=x(j)+l;
end
else
for j=1:n
B(j)=y(j)+l;
end
end
C=A+B;
end
But in my codes, C is only affected by A (if statement is true when i=1). How can I correct this issue?
  댓글 수: 2
Rik
Rik 2017년 2월 9일
Is it possible to just move the code to the inside of the for/if thing? If not, you could maybe use a flagging variable, so you can determine where your code has gone and if A should play a role.
Rik
Rik 2017년 2월 9일
I guess this change doesn't fix your problem?
BTW, I meant something like
if test1==true
for i=1:m
A=A+1;
end
C=A+B;
else
C=B;
end

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

답변 (1개)

Jan
Jan 2017년 2월 9일
편집: Jan 2017년 2월 9일
It is not clear to me, what you want to achieve. But start with simplifying your code:
for i=1:v
if z==i
A(1:m) = x(1:m) + l;
else
B(1:n) = y(1:n) + l;
end
C = A + B;
end
All I see now is this code and your explanation, that you "need both arrays to initialize another array" and "C is only affected by A (if statement is true when i=1)". For a suggestion how to "correct" this, it is required to explain, which behavior you consider as correct.

카테고리

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