"Paul " <sweeprower@yahoo.com> wrote in message <img8ik$cba$1@fred.mathworks.com>...
> All,
> Would appreciate some help here:
> Just trying to expand my MATLAB knowledge. I have a problem from a help book that I cant figure:
> I have two vectors
> x=[1 7 8 6 5 7 3 5 4]
> e=[2 6 10 4 3 6 1 2 3]
> the formula states that when an element of e <5 then you add it to the next element, if it is still less than 5 then you add it to the next number all numbers added together are removed from the modified vect, so
> emodified would look like
> emod=[8 10 7 6 6]
> I understand its an for loop with if else statements but I cant get the vector to shorten essentially I get
> e=[8 6 10 7 3 6 3 5 3]
> the problem gives a hint that says test with cumsum but I am failing to see how that will help
> Thanks
> Paul
          
You shouldn't attempt to put your answers back into 'e'. Create a new vector and call it 'emod'. Make a forloop which accumulates a sum from the 'e' elements. The forloop index will serve to access the 'e' elements but you will have to maintain another independent index to access 'emod'. When your accumulated sum becomes greater than or equal to 5, you put that sum into 'emod', reset the accumulated sum to zero, advance the 'emod' index, and then continue.
As for the 'cumsum' hint, ask yourself what can be said about the size of elements of cumsum(emod) if you have done as above.
If you have reached the end of 'e' and the accumulated sum is still less than 5, should it be put into 'emod' or not? You will have to decide that based on your reading of the problem.
You haven't explained what the 'x' vector has to do with the problem.
Roger Stafford
