I wanted to show you a glimpse of some of the new math functionality available in R2014b.
Recently on the MATLAB newsgroup, Christoph asked this question:
I have a vector A shown below, which has 6 elements. the elements are already sorted in descending order. now i want to create vector C by deleting elements from A, starting with element a1, until the sum of the vector equals or is smaller the value B
A= 26 23 20 19 15 14
So, the output should be
C= 20 19 15 14
Any idea how to do this?
I would do something like this. Get the numbers in increasing order and perform a cumulative sum.
Aflip = flipud(A); Asums = cumsum(Aflip);
Find the first sum that is GREATER than b. The next element is where you want to start. startIndex = find(Asums > b, 1, 'first');
But things are reversed now. So we need to find the starting index into the original array. Aind = length(A) - startIndex + 1;
Now delete the leading values you don’t want.
Afiltered = A((Aind+1):end);
In R2014b, the function cumsum has a new option, direction. (So do some other functions in the "|cum|" category.) You can specify to do the calculation in the default or 'forward' direction, or in 'reverse'. Here's what the new solution looks like.
A = [ 26 23 20 19 15 14]; B = 70; tmpA = cumsum(A,'reverse') ind = find(tmpA > B, 1,'last') C = A; C(1:ind) = 
tmpA = 117 91 68 48 29 14 ind = 2 C = 20 19 15 14
Have you had a chance to explore some of the math functionality in R2014b? Which features did you find helpful? Let me know here.
Get the MATLAB code
Published with MATLAB® R2014b
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.