Cumulative total of columns in a matrix or table

Clash Royale CLAN TAG#URR8PPP
up vote
4
down vote
favorite
I have the following:
matrix1 = a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p
(Note that it's the data that matters, not the fact it's defined as a matrix. Could be a table, could be a list.)
I want to create a cumulative sum, so that each entry is the sum of all data in that row up to and including column n, which is the column where the sum is to be placed. In other words, I want
matrix2 = a, a + b, a + b + c, a + b + c + d, e, e + f, e + f + g, e + f + g + h, i, i + j, i + j + k, i + j + k + l, m, m + n,
m + n + o, m + n + o + p
I assume there must be a way to do this using Total[matrix1 ,2] combined with some way to specify the number of columns to sum - maybe Range...? Or should I be using Part and Span? If so, how?
Can anyone help with this? Thanks in advance.
list-manipulation matrix table summation
add a comment |Â
up vote
4
down vote
favorite
I have the following:
matrix1 = a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p
(Note that it's the data that matters, not the fact it's defined as a matrix. Could be a table, could be a list.)
I want to create a cumulative sum, so that each entry is the sum of all data in that row up to and including column n, which is the column where the sum is to be placed. In other words, I want
matrix2 = a, a + b, a + b + c, a + b + c + d, e, e + f, e + f + g, e + f + g + h, i, i + j, i + j + k, i + j + k + l, m, m + n,
m + n + o, m + n + o + p
I assume there must be a way to do this using Total[matrix1 ,2] combined with some way to specify the number of columns to sum - maybe Range...? Or should I be using Part and Span? If so, how?
Can anyone help with this? Thanks in advance.
list-manipulation matrix table summation
add a comment |Â
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I have the following:
matrix1 = a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p
(Note that it's the data that matters, not the fact it's defined as a matrix. Could be a table, could be a list.)
I want to create a cumulative sum, so that each entry is the sum of all data in that row up to and including column n, which is the column where the sum is to be placed. In other words, I want
matrix2 = a, a + b, a + b + c, a + b + c + d, e, e + f, e + f + g, e + f + g + h, i, i + j, i + j + k, i + j + k + l, m, m + n,
m + n + o, m + n + o + p
I assume there must be a way to do this using Total[matrix1 ,2] combined with some way to specify the number of columns to sum - maybe Range...? Or should I be using Part and Span? If so, how?
Can anyone help with this? Thanks in advance.
list-manipulation matrix table summation
I have the following:
matrix1 = a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p
(Note that it's the data that matters, not the fact it's defined as a matrix. Could be a table, could be a list.)
I want to create a cumulative sum, so that each entry is the sum of all data in that row up to and including column n, which is the column where the sum is to be placed. In other words, I want
matrix2 = a, a + b, a + b + c, a + b + c + d, e, e + f, e + f + g, e + f + g + h, i, i + j, i + j + k, i + j + k + l, m, m + n,
m + n + o, m + n + o + p
I assume there must be a way to do this using Total[matrix1 ,2] combined with some way to specify the number of columns to sum - maybe Range...? Or should I be using Part and Span? If so, how?
Can anyone help with this? Thanks in advance.
list-manipulation matrix table summation
list-manipulation matrix table summation
asked Aug 31 at 6:34
Richard Burke-Ward
3839
3839
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
I first came up with:
FoldList[Plus, #[[1]], Rest[#]] & /@ matrix
Then read the documentation for Accumulate, which says it is equivalent to:
Rest[FoldList[Plus, 0, #]] & /@ matrix1
One nice thing about these approaches is they generalize beyond sums:
FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
Rest[FoldList[Times, 1, #]] & /@ matrix1
add a comment |Â
up vote
3
down vote
Just for fun, another approach using matrix-matrix multiplication:
acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
acc // MatrixForm
$left(
beginarraycccc
a & a+b & a+b+c & a+b+c+d \
e & e+f & e+f+g & e+f+g+h \
i & i+j & i+j+k & i+j+k+l \
m & m+n & m+n+o & m+n+o+p \
endarray
right)$
As alternative to Accumulate[matrix1], you could use
acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
acc // MatrixForm
$left(
beginarraycccc
a & b & c & d \
a+e & b+f & c+g & d+h \
a+e+i & b+f+j & c+g+k & d+h+l \
a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
endarray
right)$
I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.
Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
â Henrik Schumacher
Aug 31 at 9:53
Clever.Accumulateis probably the standard way, but I do prefer/like Linear Algebra solutions.
â Anton Antonov
Aug 31 at 12:43
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
I first came up with:
FoldList[Plus, #[[1]], Rest[#]] & /@ matrix
Then read the documentation for Accumulate, which says it is equivalent to:
Rest[FoldList[Plus, 0, #]] & /@ matrix1
One nice thing about these approaches is they generalize beyond sums:
FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
Rest[FoldList[Times, 1, #]] & /@ matrix1
add a comment |Â
up vote
4
down vote
accepted
I first came up with:
FoldList[Plus, #[[1]], Rest[#]] & /@ matrix
Then read the documentation for Accumulate, which says it is equivalent to:
Rest[FoldList[Plus, 0, #]] & /@ matrix1
One nice thing about these approaches is they generalize beyond sums:
FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
Rest[FoldList[Times, 1, #]] & /@ matrix1
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
I first came up with:
FoldList[Plus, #[[1]], Rest[#]] & /@ matrix
Then read the documentation for Accumulate, which says it is equivalent to:
Rest[FoldList[Plus, 0, #]] & /@ matrix1
One nice thing about these approaches is they generalize beyond sums:
FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
Rest[FoldList[Times, 1, #]] & /@ matrix1
I first came up with:
FoldList[Plus, #[[1]], Rest[#]] & /@ matrix
Then read the documentation for Accumulate, which says it is equivalent to:
Rest[FoldList[Plus, 0, #]] & /@ matrix1
One nice thing about these approaches is they generalize beyond sums:
FoldList[Times, #[[1]], Rest[#]] & /@ matrix1
Rest[FoldList[Times, 1, #]] & /@ matrix1
answered Aug 31 at 11:17
Lee
40817
40817
add a comment |Â
add a comment |Â
up vote
3
down vote
Just for fun, another approach using matrix-matrix multiplication:
acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
acc // MatrixForm
$left(
beginarraycccc
a & a+b & a+b+c & a+b+c+d \
e & e+f & e+f+g & e+f+g+h \
i & i+j & i+j+k & i+j+k+l \
m & m+n & m+n+o & m+n+o+p \
endarray
right)$
As alternative to Accumulate[matrix1], you could use
acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
acc // MatrixForm
$left(
beginarraycccc
a & b & c & d \
a+e & b+f & c+g & d+h \
a+e+i & b+f+j & c+g+k & d+h+l \
a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
endarray
right)$
I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.
Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
â Henrik Schumacher
Aug 31 at 9:53
Clever.Accumulateis probably the standard way, but I do prefer/like Linear Algebra solutions.
â Anton Antonov
Aug 31 at 12:43
add a comment |Â
up vote
3
down vote
Just for fun, another approach using matrix-matrix multiplication:
acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
acc // MatrixForm
$left(
beginarraycccc
a & a+b & a+b+c & a+b+c+d \
e & e+f & e+f+g & e+f+g+h \
i & i+j & i+j+k & i+j+k+l \
m & m+n & m+n+o & m+n+o+p \
endarray
right)$
As alternative to Accumulate[matrix1], you could use
acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
acc // MatrixForm
$left(
beginarraycccc
a & b & c & d \
a+e & b+f & c+g & d+h \
a+e+i & b+f+j & c+g+k & d+h+l \
a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
endarray
right)$
I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.
Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
â Henrik Schumacher
Aug 31 at 9:53
Clever.Accumulateis probably the standard way, but I do prefer/like Linear Algebra solutions.
â Anton Antonov
Aug 31 at 12:43
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Just for fun, another approach using matrix-matrix multiplication:
acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
acc // MatrixForm
$left(
beginarraycccc
a & a+b & a+b+c & a+b+c+d \
e & e+f & e+f+g & e+f+g+h \
i & i+j & i+j+k & i+j+k+l \
m & m+n & m+n+o & m+n+o+p \
endarray
right)$
As alternative to Accumulate[matrix1], you could use
acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
acc // MatrixForm
$left(
beginarraycccc
a & b & c & d \
a+e & b+f & c+g & d+h \
a+e+i & b+f+j & c+g+k & d+h+l \
a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
endarray
right)$
I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.
Just for fun, another approach using matrix-matrix multiplication:
acc = matrix1.UpperTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1][[2]]]];
acc // MatrixForm
$left(
beginarraycccc
a & a+b & a+b+c & a+b+c+d \
e & e+f & e+f+g & e+f+g+h \
i & i+j & i+j+k & i+j+k+l \
m & m+n & m+n+o & m+n+o+p \
endarray
right)$
As alternative to Accumulate[matrix1], you could use
acc = LowerTriangularize[ConstantArray[1, 1, 1 Dimensions[matrix1[[1]]]].matrix1;
acc // MatrixForm
$left(
beginarraycccc
a & b & c & d \
a+e & b+f & c+g & d+h \
a+e+i & b+f+j & c+g+k & d+h+l \
a+e+i+m & b+f+j+n & c+g+k+o & d+h+l+p \
endarray
right)$
I would not advise to use these in practice as they have comptational complexity $O(n^3)$ while the methods involving Accumulate have only complexity $O(n^2)$.
answered Aug 31 at 6:58
Henrik Schumacher
39.3k254117
39.3k254117
Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
â Henrik Schumacher
Aug 31 at 9:53
Clever.Accumulateis probably the standard way, but I do prefer/like Linear Algebra solutions.
â Anton Antonov
Aug 31 at 12:43
add a comment |Â
Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
â Henrik Schumacher
Aug 31 at 9:53
Clever.Accumulateis probably the standard way, but I do prefer/like Linear Algebra solutions.
â Anton Antonov
Aug 31 at 12:43
Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
â Henrik Schumacher
Aug 31 at 9:53
Dear @Richard, you can only mark a single answer as the answer to your question (although you can upvote as many as you like). In this case, I would recommend to mark kglr's post as answer as it is (i) more concise and (ii) more efficient than mine.
â Henrik Schumacher
Aug 31 at 9:53
Clever.
Accumulate is probably the standard way, but I do prefer/like Linear Algebra solutions.â Anton Antonov
Aug 31 at 12:43
Clever.
Accumulate is probably the standard way, but I do prefer/like Linear Algebra solutions.â Anton Antonov
Aug 31 at 12:43
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180956%2fcumulative-total-of-columns-in-a-matrix-or-table%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password