简体   繁体   中英

Add values in array and compare with threshold within loop in Matlab

I am stuck trying to figure this out. I have an array:

a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1]

I want to add the values in the array so that it equal to 10 . Once the added value reaches 10, I want the array to start adding the value again until it reaches 10. There is two problem that I face here,

1) How can I add the array so that the sum = 10 everytime. Notice that in the array, there is 3 . If I add all the value before 3 , I get 8 and I only need 2 from 3 . I need to make sure that the remainder, which is 1 is added to the next array to get the sum 10 .

2) How do I break the loop once it reaches 10 and ask it continue the summation to next value to get another 10 ?

I created a loop but it only works for the first part of the array. I have no idea how to make it continue. The code is as follow:

a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1]; 
c = 0; 

for i = 1:length(a) 
   while c < 10
      c = c + a(i);
   break
   end
end

Please help. Thank you

This can be done using cumsum , mod , diff and find as follows:

temp = cumsum(a);
required = find([0 diff(mod(temp,10))] <0)

cumsum returns the cumulative sum which then is rescaled using mod . diff determines where the sum gets greater than or equal to 10 and finally find determines those indexes.

Edit: Above solution works if a doesn't have negative elements. If a can have negative elements then:

temp1=cumsum(a);              %Commulative Sum
temp2=[0 diff(mod(temp1,10))];%Indexes where sum >=10 (indicated by negative values)
temp2(temp1<0)=0;             %Removing false indexes which may come if `a` has -ve values
required = find(temp2 <0)     %Required indexes

use cumsum instead of your while loop:

a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1];
a_ = a;
endidxlist = false(size(a));
startidxlist = false(size(a));
startidxlist(1) = true;
while any(a_) && (sum(a_) >= 10)
    b = cumsum(a_);
    idx = find(b >= 10,1);
    endidxlist(idx) = true;
    % move residual to the next sequence
    a_(idx) = b(idx) - 10;
    if a_(idx) > 0
        startidxlist(idx) = idx;
    elseif (idx+1) <= numel(a)
        startidxlist(idx+1) = true;
    end
    a_(1:idx-1) = 0;
end
if (idx+1) <= numel(a)
    startidxlist(idx+1) = false;
end

endidxlist gives you the end-indexes of each sequence and startidxlist the start-indexes

This should do what you are trying. It displays the index at which each time the sum equals 10. Check this with your testcases. rem stores the residual sum in each iteration which is carried forward in the next iteration. The rest of the code is similar to what you were doing.

a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1]; 
c = 0; 
rem = 0;
i = 1;
length(a);
while(i <= length(a))
   c = rem; 
   while (c < 10 && i <= length(a))
      c = c + a(i);
      i = i + 1;
      if(c >= 10)
        rem = c - 10;
        break
      end
   end
   if(c >= 10)
      disp(i-1)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM