Add values in array and compare with threshold within loop in Matlab -
i stuck trying figure out. have array:
a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1]
i want add values in array equal 10
. once added value reaches 10, want array start adding value again until reaches 10. there 2 problem face here,
1) how can add array sum = 10
everytime. notice in array, there 3
. if add value before 3
, 8
, need 2
3
. need make sure remainder, 1
added next array sum 10
.
2) how break loop once reaches 10
, ask continue summation next value 10
?
i created loop works first part of array. have no idea how make continue. code follow:
a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1]; c = 0; = 1:length(a) while c < 10 c = c + a(i); break end end
please help. thank you
this should trying. displays index @ each time sum equals 10. check testcases. rem
stores residual sum in each iteration carried forward in next iteration. rest of code similar doing.
a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1]; c = 0; rem = 0; = 1; length(a); while(i <= length(a)) c = rem; while (c < 10 && <= length(a)) c = c + a(i); = + 1; if(c >= 10) rem = c - 10; break end end if(c >= 10) disp(i-1) end
Comments
Post a Comment