0

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

4
  • What will be the output for the given a ? Commented Apr 7, 2017 at 7:01
  • @SardarUsama I am not clear of your question. I initialize a to array with specified value, as in the code, so if I run a in Matlab, it will give me those specified values. Commented Apr 7, 2017 at 7:03
  • It is one the issues that I am facing, I need to add the add the array values so that once the sum of the array values equal to 10, I would identify the index of the last array that contribute to the sum. So in array a, I have a[1] until a[8] that contribute to sum equal to 10. However, as mentioned point (1) of the question, there will be remainder in a[8], which is 1. I want to add the remainder to the the next value in a[9] to get another sum equal to 10. I am not sure if my explaination is clear or not but I hope I can find some answers from this. Commented Apr 7, 2017 at 7:15
  • @SardarUsama the expected output is [8,15] Commented Apr 7, 2017 at 7:29

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @SardarUsama
1

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

Comments

1

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

1 Comment

good point, I edited my answer to use pre-allocated binary arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.