How can I find the cumulative sum of a vector without using the cumsum funtion or any loops

38 Ansichten (letzte 30 Tage)
If I have [4 -2 6 1 2], how I find the cum sum of this vector without using the cumsum function or loops or any other functions. I method I am allowed to use is just simple vector operations (sum,diff,etc) functions.
  3 Kommentare
Image Analyst
Image Analyst am 13 Sep. 2017
What's wrong with the totally obvious:
theSum = 4 + -2 + 6 + 1 + 2;
That is one way that works without loops or any functions. It meets all your stated requirements. Anything wrong with it?
Stephen23
Stephen23 am 19 Sep. 2017
Bearbeitet: Stephen23 am 19 Sep. 2017
"It meets all your stated requirements"
All except for the "cumulative sum" part.

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Tim Berk
Tim Berk am 19 Sep. 2017
I know this is your homework, but I thought it was a fun problem.
You can do this with matrix manipulations:
A = [4 -2 6 1 2]
B = tril(ones(length(A)))
C = A.*B
cum_sum = sum(C,2)

Stephen23
Stephen23 am 19 Sep. 2017
>> A = [4,-2,6,1,2];
>> sum(triu(A(:)*ones(1,numel(A))),1)
ans =
4 2 8 9 11
>> cumsum(A)
ans =
4 2 8 9 11
  6 Kommentare
Tim Berk
Tim Berk am 20 Sep. 2017
"You would also need to transpose B to get the right answer:"
That's right. Or using triu instead of tril.
Since you seem to be interested in small details: technically your code does not work for "any size or orientation array". It only gives the right answer for row vectors, as the answer is always a row vector. If you input a column vector, the answer is the transpose of the actual cumsum. If you input a 2D array the answer is a row vector that has nothing to do with the cumsum of that array.
The cumulative sum of
A = [1 2; 3 4]
Should be
[1 2; 4 6]
or
[1 3; 3 7]
depending on which dimension we are taking the cumulative sum over. Neither of our methods gives such an answer.
Stephen23
Stephen23 am 20 Sep. 2017
Bearbeitet: Stephen23 am 21 Sep. 2017
@Tim Berk: Good catch, you are right, I was wrong that my code works with "any size ... input array".

Melden Sie sich an, um zu kommentieren.


Andrei Bobrov
Andrei Bobrov am 20 Sep. 2017
sum(hankel([zeros(numel(A)-1,1);A(1)],A))

the cyclist
the cyclist am 20 Sep. 2017
Perhaps the only solution which will not raise the eyebrows of OP's instructor:
A = [4 -2 6 1 2];
nA = numel(A);
cumA = zeros(1,nA);
for ia = 1:nA
cumA(ia) = sum(A(1:ia));
end
:-)
  2 Kommentare
Tim Berk
Tim Berk am 20 Sep. 2017
Depends on whether it is a course on matlab or on vector calculus.
In any case, a for loop might not be a good idea for large nA. If I were to instruct a course on matlab, I would prefer students to work with the fastest solution.
the cyclist
the cyclist am 21 Sep. 2017
The for loop is fast for moderately large nA. In fact, it is the fastest of the three solutions that I tried (in admittedly little testing on my part). For very large nA, there might also be memory issues for some solutions.
rng default
N = 5000;
NR = 100;
A = rand(1,N);
tic
for ii = 1:NR
nA = numel(A);
cumA1 = zeros(1,nA);
for ia = 1:nA
cumA1(ia) = sum(A(1:ia));
end
end
toc
tic
for ii = 1:NR
B = tril(ones(length(A)));
C = A.*B;
cumA2 = sum(C,2);
end
toc
tic
for ii = 1:NR
cumA3 = sum(triu(A(:)*ones(1,numel(A))),1);
end
toc

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by