Updating a value inside for loop
조회 수: 11 (최근 30일)
이전 댓글 표시
Hi All,
I have a for loop inside which, various operations are running. There are a few variable inside that loop which I want to update after every 4 iterations of the loop.
Lets say we have this situation
a = 1:100
X= X+Y
....
....
Y = 123;
end
I want this Y to update after 4 iterations as in, for a=1:4, the value of Y remains same and for a=5:8, it remains same and so on.
How should I perform this.
Regards
Anum
댓글 수: 0
채택된 답변
David K.
2019년 9월 13일
A common way people do things like this is with an if statement. For the condition of the if statement you need to relate it to a. There is a function called modulus which outputs the remainder of a division between two numbers. This can be used as such:
for a = 1:100
if(mod(a,4)==0)
y = updatedY
end
end
Depending on what you want you may need to change the 4 to a 5.
The way modulus works is that every time a is a multiple of 4 the output will be 0 and so y will be updated.
댓글 수: 0
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!