Help with sum specific elements of a matrix in mathematica
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I am trying to sum over specific indices in a matrix.
For example, if I have the matrix
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7
i need to sum the index 2, 1
and 4, 3
, i.e 4 + 9 = 13
automatically.
I try the code
Sum[a[[ii]][[jj]], ii, 2, 4, 2, jj, 1, 4 , 2]
and this is equals to 20. (WTF) (i think this shows the result of 4+6+1+9) Why?
In a general form I need to compute the sum of indices 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14,13, 16,15
for a 16x16 square matrix.
Thanks for the help
Regards
matrix
migrated from stats.stackexchange.com 50 mins ago
This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.
add a comment |Â
up vote
1
down vote
favorite
I am trying to sum over specific indices in a matrix.
For example, if I have the matrix
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7
i need to sum the index 2, 1
and 4, 3
, i.e 4 + 9 = 13
automatically.
I try the code
Sum[a[[ii]][[jj]], ii, 2, 4, 2, jj, 1, 4 , 2]
and this is equals to 20. (WTF) (i think this shows the result of 4+6+1+9) Why?
In a general form I need to compute the sum of indices 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14,13, 16,15
for a 16x16 square matrix.
Thanks for the help
Regards
matrix
migrated from stats.stackexchange.com 50 mins ago
This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to sum over specific indices in a matrix.
For example, if I have the matrix
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7
i need to sum the index 2, 1
and 4, 3
, i.e 4 + 9 = 13
automatically.
I try the code
Sum[a[[ii]][[jj]], ii, 2, 4, 2, jj, 1, 4 , 2]
and this is equals to 20. (WTF) (i think this shows the result of 4+6+1+9) Why?
In a general form I need to compute the sum of indices 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14,13, 16,15
for a 16x16 square matrix.
Thanks for the help
Regards
matrix
I am trying to sum over specific indices in a matrix.
For example, if I have the matrix
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7
i need to sum the index 2, 1
and 4, 3
, i.e 4 + 9 = 13
automatically.
I try the code
Sum[a[[ii]][[jj]], ii, 2, 4, 2, jj, 1, 4 , 2]
and this is equals to 20. (WTF) (i think this shows the result of 4+6+1+9) Why?
In a general form I need to compute the sum of indices 2, 1, 4, 3, 6, 5, 8, 7, 10, 9, 12, 11, 14,13, 16,15
for a 16x16 square matrix.
Thanks for the help
Regards
matrix
matrix
edited 37 mins ago
kglr
165k8188388
165k8188388
asked 1 hour ago
Santiago MarÃn Agudelo
migrated from stats.stackexchange.com 50 mins ago
This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.
migrated from stats.stackexchange.com 50 mins ago
This question came from our site for people interested in statistics, machine learning, data analysis, data mining, and data visualization.
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
4
down vote
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
positions = 2, 1, 4, 3;
Total@Extract[a, positions]
add a comment |Â
up vote
2
down vote
To see why it doesn't work as you expected, look at the indices that you generated.
Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]
(* 2, 1, 2, 3, 4, 1, 4, 3 *)
You generated four indices so the sum was over four entries. Use
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]
(* 13 *)
add a comment |Â
up vote
1
down vote
I would have used Extract
if Alan had not posted it first.
So, here is a way to use Sum
:
Sum[a[[## & @@ i]], i, 2, 1, 4, 3]
13
But that is also posted a minute earlier by Bob Hanlon.
That leaves
⺠= â¯, â¯â¯ [Function] +## & @@ (â¯[[##]] & @@@ â¯â¯);
âº[a, 2, 1, 4, 3]
13
and
âºâº = +## & @@ (⯠[Function] #[[## & @@ â¯]]) /@ #2 &;
âºâº[a, 2, 1, 4, 3]
13
"A monad is just a monoid in the category of endofunctors, what's the problem?"
â kglr
8 mins ago
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
positions = 2, 1, 4, 3;
Total@Extract[a, positions]
add a comment |Â
up vote
4
down vote
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
positions = 2, 1, 4, 3;
Total@Extract[a, positions]
add a comment |Â
up vote
4
down vote
up vote
4
down vote
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
positions = 2, 1, 4, 3;
Total@Extract[a, positions]
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
positions = 2, 1, 4, 3;
Total@Extract[a, positions]
answered 1 hour ago
Alan
5,8691023
5,8691023
add a comment |Â
add a comment |Â
up vote
2
down vote
To see why it doesn't work as you expected, look at the indices that you generated.
Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]
(* 2, 1, 2, 3, 4, 1, 4, 3 *)
You generated four indices so the sum was over four entries. Use
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]
(* 13 *)
add a comment |Â
up vote
2
down vote
To see why it doesn't work as you expected, look at the indices that you generated.
Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]
(* 2, 1, 2, 3, 4, 1, 4, 3 *)
You generated four indices so the sum was over four entries. Use
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]
(* 13 *)
add a comment |Â
up vote
2
down vote
up vote
2
down vote
To see why it doesn't work as you expected, look at the indices that you generated.
Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]
(* 2, 1, 2, 3, 4, 1, 4, 3 *)
You generated four indices so the sum was over four entries. Use
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]
(* 13 *)
To see why it doesn't work as you expected, look at the indices that you generated.
Table[ii, jj, ii, 2, 4, 2, jj, 1, 4, 2]
(* 2, 1, 2, 3, 4, 1, 4, 3 *)
You generated four indices so the sum was over four entries. Use
a = 1, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 1, 5, 9, 7;
Sum[a[[Sequence @@ i]], i, 2, 1, 4, 3]
(* 13 *)
answered 21 mins ago
Bob Hanlon
56.2k23590
56.2k23590
add a comment |Â
add a comment |Â
up vote
1
down vote
I would have used Extract
if Alan had not posted it first.
So, here is a way to use Sum
:
Sum[a[[## & @@ i]], i, 2, 1, 4, 3]
13
But that is also posted a minute earlier by Bob Hanlon.
That leaves
⺠= â¯, â¯â¯ [Function] +## & @@ (â¯[[##]] & @@@ â¯â¯);
âº[a, 2, 1, 4, 3]
13
and
âºâº = +## & @@ (⯠[Function] #[[## & @@ â¯]]) /@ #2 &;
âºâº[a, 2, 1, 4, 3]
13
"A monad is just a monoid in the category of endofunctors, what's the problem?"
â kglr
8 mins ago
add a comment |Â
up vote
1
down vote
I would have used Extract
if Alan had not posted it first.
So, here is a way to use Sum
:
Sum[a[[## & @@ i]], i, 2, 1, 4, 3]
13
But that is also posted a minute earlier by Bob Hanlon.
That leaves
⺠= â¯, â¯â¯ [Function] +## & @@ (â¯[[##]] & @@@ â¯â¯);
âº[a, 2, 1, 4, 3]
13
and
âºâº = +## & @@ (⯠[Function] #[[## & @@ â¯]]) /@ #2 &;
âºâº[a, 2, 1, 4, 3]
13
"A monad is just a monoid in the category of endofunctors, what's the problem?"
â kglr
8 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I would have used Extract
if Alan had not posted it first.
So, here is a way to use Sum
:
Sum[a[[## & @@ i]], i, 2, 1, 4, 3]
13
But that is also posted a minute earlier by Bob Hanlon.
That leaves
⺠= â¯, â¯â¯ [Function] +## & @@ (â¯[[##]] & @@@ â¯â¯);
âº[a, 2, 1, 4, 3]
13
and
âºâº = +## & @@ (⯠[Function] #[[## & @@ â¯]]) /@ #2 &;
âºâº[a, 2, 1, 4, 3]
13
I would have used Extract
if Alan had not posted it first.
So, here is a way to use Sum
:
Sum[a[[## & @@ i]], i, 2, 1, 4, 3]
13
But that is also posted a minute earlier by Bob Hanlon.
That leaves
⺠= â¯, â¯â¯ [Function] +## & @@ (â¯[[##]] & @@@ â¯â¯);
âº[a, 2, 1, 4, 3]
13
and
âºâº = +## & @@ (⯠[Function] #[[## & @@ â¯]]) /@ #2 &;
âºâº[a, 2, 1, 4, 3]
13
edited 9 mins ago
answered 20 mins ago
kglr
165k8188388
165k8188388
"A monad is just a monoid in the category of endofunctors, what's the problem?"
â kglr
8 mins ago
add a comment |Â
"A monad is just a monoid in the category of endofunctors, what's the problem?"
â kglr
8 mins ago
"A monad is just a monoid in the category of endofunctors, what's the problem?"
â kglr
8 mins ago
"A monad is just a monoid in the category of endofunctors, what's the problem?"
â kglr
8 mins ago
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%2f183720%2fhelp-with-sum-specific-elements-of-a-matrix-in-mathematica%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