accumulate

Understanding std::accumulate

无人久伴 提交于 2019-11-28 18:51:42
问题 I want to know why std::accumulate (aka reduce) 3rd parameter is needed. For those who do not know what accumulate is, it's used like so: vector<int> V{1,2,3}; int sum = accumulate(V.begin(), V.end(), 0); // sum == 6 Call to accumulate is equivalent to: sum = 0; // 0 - value of 3rd param for (auto x : V) sum += x; There is also optional 4th parameter, which allow to replace addition with any other operation. Rationale that I've heard is that if you need let say not to add up, but multiply

Java8 stream.reduce() with 3 parameters - getting transparency

与世无争的帅哥 提交于 2019-11-28 11:20:07
I wrote this code to reduce a list of words to a long count of how many words start with an 'A'. I'm just writing it to learn Java 8, so I'd like to understand it a little better [Disclaimer: I realize this is probably not the best way to write this code; it's just for practice!] . Long countOfAWords = results.stream().reduce( 0L, (a, b) -> b.charAt(0) == 'A' ? a + 1 : a, Long::sum); The middle parameter/lambda (called the accumulator) would seem to be capable of reducing the full list without the final 'Combiner' parameter. In fact, the Javadoc actually says: The {@code accumulator} function

Fun depending on order of subs and values in accumarray

梦想与她 提交于 2019-11-28 01:28:56
In accumarray() the first note about 'subs', first appeared in the MATLAB R14sp3 docs , says: Note If the subscripts in subs are not sorted, fun should not depend on the order of the values in its input data. It is not clear to me what is considered to be sorted. Suppose: subs = [1 1 2 1 1 2 2 2]; shall subs be sorted in the sense issorted(subs,'rows') , or ... in the linear indexing sense, i.e. issorted(sub2ind([2 2],subs(:,1), subs(:,2))) I would like to rely on: accumarray(subs,val,[], @(x) x(end)) If somebody could also provide examples/tests from older releases (to check for backward

MatLab accumarray unexpectedly changing ordering

风格不统一 提交于 2019-11-27 09:36:31
As long as I understood accumarray, it means "Making the nth row of the output: 1) find n in sub. 2) if n is in m1, m2, m3 th element in sub, 3) apply the function to m1,m2,m3 th element of val 4) that's the nth row of the output" Am I wrong somewhere? I ran the following code. A = [2 10 13 ; 1 11 14; 1 12 10] [U,ix,iu]= unique(A(:,1)) vals = reshape(A(:, 2:end).', [], 1) subs = reshape(iu(:, ones(size(A, 2)-1,1)).', [], 1) r2 = accumarray(subs, vals', [], @(x){x'}) r2{1} r2{2} A = 2 10 13 1 11 14 1 12 10 U = 1 2 ix = 3 1 iu = 2 1 1 vals = 10 13 11 14 12 10 subs = 2 2 1 1 1 1 r2 = [1x4 double]

Java8 stream.reduce() with 3 parameters - getting transparency

半世苍凉 提交于 2019-11-27 06:14:27
问题 I wrote this code to reduce a list of words to a long count of how many words start with an 'A'. I'm just writing it to learn Java 8, so I'd like to understand it a little better [Disclaimer: I realize this is probably not the best way to write this code; it's just for practice!] . Long countOfAWords = results.stream().reduce( 0L, (a, b) -> b.charAt(0) == 'A' ? a + 1 : a, Long::sum); The middle parameter/lambda (called the accumulator) would seem to be capable of reducing the full list

Why is the std::accumulate function showing the wrong sum of a vector<double>?

巧了我就是萌 提交于 2019-11-26 23:13:10
问题 Consider the following code for adding all the elements of a vector : #include<iostream> #include<algorithm> #include<numeric> #include<vector> using namespace std; int main(void) { std::vector<double> V; V.push_back(1.2); V.push_back(3.6); V.push_back(5.6); double sum = accumulate(V.begin(),V.end(),0); cout << "The sum of the elements of the vector V is " << sum << endl; return 0; } When I compile this on Cygwin on Windows and run it, I get the output at the terminal as The sum of the

Fun depending on order of subs and values in accumarray

孤者浪人 提交于 2019-11-26 21:55:24
问题 In accumarray() the first note about 'subs', first appeared in the MATLAB R14sp3 docs, says: Note If the subscripts in subs are not sorted, fun should not depend on the order of the values in its input data. It is not clear to me what is considered to be sorted. Suppose: subs = [1 1 2 1 1 2 2 2]; shall subs be sorted in the sense issorted(subs,'rows') , or ... in the linear indexing sense, i.e. issorted(sub2ind([2 2],subs(:,1), subs(:,2))) I would like to rely on: accumarray(subs,val,[], @(x)

MatLab accumarray unexpectedly changing ordering

廉价感情. 提交于 2019-11-26 17:51:11
问题 As long as I understood accumarray, it means "Making the nth row of the output: 1) find n in sub. 2) if n is in m1, m2, m3 th element in sub, 3) apply the function to m1,m2,m3 th element of val 4) that's the nth row of the output" Am I wrong somewhere? I ran the following code. A = [2 10 13 ; 1 11 14; 1 12 10] [U,ix,iu]= unique(A(:,1)) vals = reshape(A(:, 2:end).', [], 1) subs = reshape(iu(:, ones(size(A, 2)-1,1)).', [], 1) r2 = accumarray(subs, vals', [], @(x){x'}) r2{1} r2{2} A = 2 10 13 1

How to find the cumulative sum of numbers in a list?

半世苍凉 提交于 2019-11-25 22:38:27
问题 time_interval = [4, 6, 12] I want to sum up the numbers like [4, 4+6, 4+6+12] in order to get the list t = [4, 10, 22] . I tried the following: for i in time_interval: t1 = time_interval[0] t2 = time_interval[1] + t1 t3 = time_interval[2] + t2 print(t1, t2, t3) 4 10 22 4 10 22 4 10 22 回答1: If you're doing much numerical work with arrays like this, I'd suggest numpy, which comes with a cumulative sum function cumsum: import numpy as np a = [4,6,12] np.cumsum(a) #array([4, 10, 22]) Numpy is