How to add just the second elements in lists of pairs?How to create a list of pairs from 1d...
Test if two foods are the same
How can I replicate this effect of the Infinity Gauntlet using official material?
Match the blocks
Change computer name Ubuntu
What is the word for things that work even when they aren't working - e.g escalators?
What good is Divine Sense?
What is this dial on my old film camera for?
UK PM is taking his proposal to EU but has not proposed to his own parliament - can he legally bypass the UK parliament?
How to respond when insulted by a grad student in a different department?
Is it possible to do a low carb diet for a month in Sicily?
An employee has low self-confidence, and is performing poorly. How can I help?
"Kept that sister of his quiet" meaning
How do express my condolences, when I couldn't show up at the funeral?
SSH from a shared workplace computer
Applying rules on rules
My professor says my digit summing code is flawed. Is he right?
My name was added to manuscript as co-author without my consent; how to get it removed?
Multiple stock symbols for same company with in USA
How to execute a project with two resources where you need three resources?
Prove the inequality is true
What does IKEA-like mean?
First aid scissors confiscated by Dubai airport security
What are the different ways one can refer to the home in everyday French
Does these arithmetic means on Pythagorean triangles converge?
How to add just the second elements in lists of pairs?
How to create a list of pairs from 1d list(s)?Generating an ordered list of pairs of elements from ordered listsHow can I select elements that are trueHow to combine the elements of two lists using a user-specified functionHow can I make a lists of pairs out of two lists of triples?Subtract second element of element of list from other list if the first elements are equalCombinations of a variable number of lists with different number of elementsAdding a number to the 2nd element of each pair in a list of pairs
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{
margin-bottom:0;
}
.everyonelovesstackoverflow{position:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;}
$begingroup$
Say I have two lists of pairs
a = {{1, 2}, {3, 4}};
b = {{5, 6}, {7, 8}};
and I want to add the 2nd element of every pair in b
to the 2nd element of every pair in a
, so the expected result would be {{1, 8}, {3, 12}}
.
How can I do this?
I am a total beginner with Mathematica, so sorry if this is to basic :)
list-manipulation
New contributor
$endgroup$
add a comment
|
$begingroup$
Say I have two lists of pairs
a = {{1, 2}, {3, 4}};
b = {{5, 6}, {7, 8}};
and I want to add the 2nd element of every pair in b
to the 2nd element of every pair in a
, so the expected result would be {{1, 8}, {3, 12}}
.
How can I do this?
I am a total beginner with Mathematica, so sorry if this is to basic :)
list-manipulation
New contributor
$endgroup$
add a comment
|
$begingroup$
Say I have two lists of pairs
a = {{1, 2}, {3, 4}};
b = {{5, 6}, {7, 8}};
and I want to add the 2nd element of every pair in b
to the 2nd element of every pair in a
, so the expected result would be {{1, 8}, {3, 12}}
.
How can I do this?
I am a total beginner with Mathematica, so sorry if this is to basic :)
list-manipulation
New contributor
$endgroup$
Say I have two lists of pairs
a = {{1, 2}, {3, 4}};
b = {{5, 6}, {7, 8}};
and I want to add the 2nd element of every pair in b
to the 2nd element of every pair in a
, so the expected result would be {{1, 8}, {3, 12}}
.
How can I do this?
I am a total beginner with Mathematica, so sorry if this is to basic :)
list-manipulation
list-manipulation
New contributor
New contributor
edited 4 hours ago
m_goldberg
91.9k8 gold badges75 silver badges210 bronze badges
91.9k8 gold badges75 silver badges210 bronze badges
New contributor
asked 8 hours ago
JimJim
213 bronze badges
213 bronze badges
New contributor
New contributor
add a comment
|
add a comment
|
6 Answers
6
active
oldest
votes
$begingroup$
lst1 = {{1, 2}, {2, 4}};
lst1a = lst1;
lst1a[[All, 2]] *= 2;
lst1a
{{1, 4}, {2, 8}}
lst2 = {{3, 5}, {10, 2}};
lst1b = lst1;
lst1b[[All, 2]] = lst1[[All, 2]] + lst2[[All, 2]];
lst1b
{{1, 7}, {2, 6}}
$endgroup$
add a comment
|
$begingroup$
Try this:
{{1, 2}, {2, 4}} + ({{1, 2}, {2, 4}} /. {x_,y _} -> {0, y})
(* {{2, 4}, {4, 8}} *)
or this
(# + (# /. {x_, y_} -> {0, y})) &[{{1, 2}, {2, 4}}]
(* {{1, 2}, {4, 8}} *)
Have fun!
$endgroup$
add a comment
|
$begingroup$
list = {{1, 2}, {2, 4}};
MapAt[2 # &, {All, 2}] @ list
{{1, 4}, {2, 8}}
$endgroup$
$begingroup$
Hi @eldo! It's been a while... :)
$endgroup$
– Michael E2
7 hours ago
$begingroup$
Yes, nice to see you again :}
$endgroup$
– eldo
7 hours ago
add a comment
|
$begingroup$
lst = {{1, 2}, {2, 4}};
MapThread[{First[#1], Last[#1] + Last[#2]} &, {lst, lst}]
{{1, 4}, {2, 8}}
$endgroup$
add a comment
|
$begingroup$
I like to do this kind of thing by writing a custom function to perform the the desired operation on two given pairs. I then map this function over the two lists with MapThread
. In most cases, by using Mathematica's argument-pattern destructuring, the custom function can be expressed extremely simply. This is one of those cases.
The custom function is
f[{x_, y_}, {_, z_}] := {x, y + z}
Note that the righthand side is a literal expression of the desired result.
Now let's contrive some test data. I write the generator so that two lists of pairs can easily be given any length.
With[{n = 3}, {a, b} = Partition[Partition[Range[4 n], 2], n]];
a
b
{{1, 2}, {3, 4}, {5, 6}}
{{7, 8}, {9, 10}, {11, 12}}
Now we MapThread
the function f
over a
and b
.
MapThread[f, {a, b}]
{1, 10}, {3, 14}, {5, 18}}
$endgroup$
add a comment
|
$begingroup$
list1 = {{1, 2}, {2, 4}};
Adding the second elements from each pair of the same list:
{#[[1]], 2 #[[2]]} & /@ list1
{{1, 4}, {2, 8}}
Adding the second element from a different list:
list2 = {{a, b}, {c, d}};
{#[[1, 1]], #[[1, 2]] + #[[2, 2]]} & /@ Transpose[{list1, list2}]
{{1, 2 + b}, {2, 4 + d}}
$endgroup$
add a comment
|
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Jim is a new contributor. Be nice, and check out our Code of Conduct.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f207289%2fhow-to-add-just-the-second-elements-in-lists-of-pairs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
lst1 = {{1, 2}, {2, 4}};
lst1a = lst1;
lst1a[[All, 2]] *= 2;
lst1a
{{1, 4}, {2, 8}}
lst2 = {{3, 5}, {10, 2}};
lst1b = lst1;
lst1b[[All, 2]] = lst1[[All, 2]] + lst2[[All, 2]];
lst1b
{{1, 7}, {2, 6}}
$endgroup$
add a comment
|
$begingroup$
lst1 = {{1, 2}, {2, 4}};
lst1a = lst1;
lst1a[[All, 2]] *= 2;
lst1a
{{1, 4}, {2, 8}}
lst2 = {{3, 5}, {10, 2}};
lst1b = lst1;
lst1b[[All, 2]] = lst1[[All, 2]] + lst2[[All, 2]];
lst1b
{{1, 7}, {2, 6}}
$endgroup$
add a comment
|
$begingroup$
lst1 = {{1, 2}, {2, 4}};
lst1a = lst1;
lst1a[[All, 2]] *= 2;
lst1a
{{1, 4}, {2, 8}}
lst2 = {{3, 5}, {10, 2}};
lst1b = lst1;
lst1b[[All, 2]] = lst1[[All, 2]] + lst2[[All, 2]];
lst1b
{{1, 7}, {2, 6}}
$endgroup$
lst1 = {{1, 2}, {2, 4}};
lst1a = lst1;
lst1a[[All, 2]] *= 2;
lst1a
{{1, 4}, {2, 8}}
lst2 = {{3, 5}, {10, 2}};
lst1b = lst1;
lst1b[[All, 2]] = lst1[[All, 2]] + lst2[[All, 2]];
lst1b
{{1, 7}, {2, 6}}
answered 8 hours ago
kglrkglr
220k10 gold badges250 silver badges504 bronze badges
220k10 gold badges250 silver badges504 bronze badges
add a comment
|
add a comment
|
$begingroup$
Try this:
{{1, 2}, {2, 4}} + ({{1, 2}, {2, 4}} /. {x_,y _} -> {0, y})
(* {{2, 4}, {4, 8}} *)
or this
(# + (# /. {x_, y_} -> {0, y})) &[{{1, 2}, {2, 4}}]
(* {{1, 2}, {4, 8}} *)
Have fun!
$endgroup$
add a comment
|
$begingroup$
Try this:
{{1, 2}, {2, 4}} + ({{1, 2}, {2, 4}} /. {x_,y _} -> {0, y})
(* {{2, 4}, {4, 8}} *)
or this
(# + (# /. {x_, y_} -> {0, y})) &[{{1, 2}, {2, 4}}]
(* {{1, 2}, {4, 8}} *)
Have fun!
$endgroup$
add a comment
|
$begingroup$
Try this:
{{1, 2}, {2, 4}} + ({{1, 2}, {2, 4}} /. {x_,y _} -> {0, y})
(* {{2, 4}, {4, 8}} *)
or this
(# + (# /. {x_, y_} -> {0, y})) &[{{1, 2}, {2, 4}}]
(* {{1, 2}, {4, 8}} *)
Have fun!
$endgroup$
Try this:
{{1, 2}, {2, 4}} + ({{1, 2}, {2, 4}} /. {x_,y _} -> {0, y})
(* {{2, 4}, {4, 8}} *)
or this
(# + (# /. {x_, y_} -> {0, y})) &[{{1, 2}, {2, 4}}]
(* {{1, 2}, {4, 8}} *)
Have fun!
answered 7 hours ago
Alexei BoulbitchAlexei Boulbitch
23k27 silver badges78 bronze badges
23k27 silver badges78 bronze badges
add a comment
|
add a comment
|
$begingroup$
list = {{1, 2}, {2, 4}};
MapAt[2 # &, {All, 2}] @ list
{{1, 4}, {2, 8}}
$endgroup$
$begingroup$
Hi @eldo! It's been a while... :)
$endgroup$
– Michael E2
7 hours ago
$begingroup$
Yes, nice to see you again :}
$endgroup$
– eldo
7 hours ago
add a comment
|
$begingroup$
list = {{1, 2}, {2, 4}};
MapAt[2 # &, {All, 2}] @ list
{{1, 4}, {2, 8}}
$endgroup$
$begingroup$
Hi @eldo! It's been a while... :)
$endgroup$
– Michael E2
7 hours ago
$begingroup$
Yes, nice to see you again :}
$endgroup$
– eldo
7 hours ago
add a comment
|
$begingroup$
list = {{1, 2}, {2, 4}};
MapAt[2 # &, {All, 2}] @ list
{{1, 4}, {2, 8}}
$endgroup$
list = {{1, 2}, {2, 4}};
MapAt[2 # &, {All, 2}] @ list
{{1, 4}, {2, 8}}
answered 7 hours ago
eldoeldo
30.4k2 gold badges28 silver badges85 bronze badges
30.4k2 gold badges28 silver badges85 bronze badges
$begingroup$
Hi @eldo! It's been a while... :)
$endgroup$
– Michael E2
7 hours ago
$begingroup$
Yes, nice to see you again :}
$endgroup$
– eldo
7 hours ago
add a comment
|
$begingroup$
Hi @eldo! It's been a while... :)
$endgroup$
– Michael E2
7 hours ago
$begingroup$
Yes, nice to see you again :}
$endgroup$
– eldo
7 hours ago
$begingroup$
Hi @eldo! It's been a while... :)
$endgroup$
– Michael E2
7 hours ago
$begingroup$
Hi @eldo! It's been a while... :)
$endgroup$
– Michael E2
7 hours ago
$begingroup$
Yes, nice to see you again :}
$endgroup$
– eldo
7 hours ago
$begingroup$
Yes, nice to see you again :}
$endgroup$
– eldo
7 hours ago
add a comment
|
$begingroup$
lst = {{1, 2}, {2, 4}};
MapThread[{First[#1], Last[#1] + Last[#2]} &, {lst, lst}]
{{1, 4}, {2, 8}}
$endgroup$
add a comment
|
$begingroup$
lst = {{1, 2}, {2, 4}};
MapThread[{First[#1], Last[#1] + Last[#2]} &, {lst, lst}]
{{1, 4}, {2, 8}}
$endgroup$
add a comment
|
$begingroup$
lst = {{1, 2}, {2, 4}};
MapThread[{First[#1], Last[#1] + Last[#2]} &, {lst, lst}]
{{1, 4}, {2, 8}}
$endgroup$
lst = {{1, 2}, {2, 4}};
MapThread[{First[#1], Last[#1] + Last[#2]} &, {lst, lst}]
{{1, 4}, {2, 8}}
answered 7 hours ago
Suba ThomasSuba Thomas
4,12611 silver badges20 bronze badges
4,12611 silver badges20 bronze badges
add a comment
|
add a comment
|
$begingroup$
I like to do this kind of thing by writing a custom function to perform the the desired operation on two given pairs. I then map this function over the two lists with MapThread
. In most cases, by using Mathematica's argument-pattern destructuring, the custom function can be expressed extremely simply. This is one of those cases.
The custom function is
f[{x_, y_}, {_, z_}] := {x, y + z}
Note that the righthand side is a literal expression of the desired result.
Now let's contrive some test data. I write the generator so that two lists of pairs can easily be given any length.
With[{n = 3}, {a, b} = Partition[Partition[Range[4 n], 2], n]];
a
b
{{1, 2}, {3, 4}, {5, 6}}
{{7, 8}, {9, 10}, {11, 12}}
Now we MapThread
the function f
over a
and b
.
MapThread[f, {a, b}]
{1, 10}, {3, 14}, {5, 18}}
$endgroup$
add a comment
|
$begingroup$
I like to do this kind of thing by writing a custom function to perform the the desired operation on two given pairs. I then map this function over the two lists with MapThread
. In most cases, by using Mathematica's argument-pattern destructuring, the custom function can be expressed extremely simply. This is one of those cases.
The custom function is
f[{x_, y_}, {_, z_}] := {x, y + z}
Note that the righthand side is a literal expression of the desired result.
Now let's contrive some test data. I write the generator so that two lists of pairs can easily be given any length.
With[{n = 3}, {a, b} = Partition[Partition[Range[4 n], 2], n]];
a
b
{{1, 2}, {3, 4}, {5, 6}}
{{7, 8}, {9, 10}, {11, 12}}
Now we MapThread
the function f
over a
and b
.
MapThread[f, {a, b}]
{1, 10}, {3, 14}, {5, 18}}
$endgroup$
add a comment
|
$begingroup$
I like to do this kind of thing by writing a custom function to perform the the desired operation on two given pairs. I then map this function over the two lists with MapThread
. In most cases, by using Mathematica's argument-pattern destructuring, the custom function can be expressed extremely simply. This is one of those cases.
The custom function is
f[{x_, y_}, {_, z_}] := {x, y + z}
Note that the righthand side is a literal expression of the desired result.
Now let's contrive some test data. I write the generator so that two lists of pairs can easily be given any length.
With[{n = 3}, {a, b} = Partition[Partition[Range[4 n], 2], n]];
a
b
{{1, 2}, {3, 4}, {5, 6}}
{{7, 8}, {9, 10}, {11, 12}}
Now we MapThread
the function f
over a
and b
.
MapThread[f, {a, b}]
{1, 10}, {3, 14}, {5, 18}}
$endgroup$
I like to do this kind of thing by writing a custom function to perform the the desired operation on two given pairs. I then map this function over the two lists with MapThread
. In most cases, by using Mathematica's argument-pattern destructuring, the custom function can be expressed extremely simply. This is one of those cases.
The custom function is
f[{x_, y_}, {_, z_}] := {x, y + z}
Note that the righthand side is a literal expression of the desired result.
Now let's contrive some test data. I write the generator so that two lists of pairs can easily be given any length.
With[{n = 3}, {a, b} = Partition[Partition[Range[4 n], 2], n]];
a
b
{{1, 2}, {3, 4}, {5, 6}}
{{7, 8}, {9, 10}, {11, 12}}
Now we MapThread
the function f
over a
and b
.
MapThread[f, {a, b}]
{1, 10}, {3, 14}, {5, 18}}
edited 34 mins ago
answered 3 hours ago
m_goldbergm_goldberg
91.9k8 gold badges75 silver badges210 bronze badges
91.9k8 gold badges75 silver badges210 bronze badges
add a comment
|
add a comment
|
$begingroup$
list1 = {{1, 2}, {2, 4}};
Adding the second elements from each pair of the same list:
{#[[1]], 2 #[[2]]} & /@ list1
{{1, 4}, {2, 8}}
Adding the second element from a different list:
list2 = {{a, b}, {c, d}};
{#[[1, 1]], #[[1, 2]] + #[[2, 2]]} & /@ Transpose[{list1, list2}]
{{1, 2 + b}, {2, 4 + d}}
$endgroup$
add a comment
|
$begingroup$
list1 = {{1, 2}, {2, 4}};
Adding the second elements from each pair of the same list:
{#[[1]], 2 #[[2]]} & /@ list1
{{1, 4}, {2, 8}}
Adding the second element from a different list:
list2 = {{a, b}, {c, d}};
{#[[1, 1]], #[[1, 2]] + #[[2, 2]]} & /@ Transpose[{list1, list2}]
{{1, 2 + b}, {2, 4 + d}}
$endgroup$
add a comment
|
$begingroup$
list1 = {{1, 2}, {2, 4}};
Adding the second elements from each pair of the same list:
{#[[1]], 2 #[[2]]} & /@ list1
{{1, 4}, {2, 8}}
Adding the second element from a different list:
list2 = {{a, b}, {c, d}};
{#[[1, 1]], #[[1, 2]] + #[[2, 2]]} & /@ Transpose[{list1, list2}]
{{1, 2 + b}, {2, 4 + d}}
$endgroup$
list1 = {{1, 2}, {2, 4}};
Adding the second elements from each pair of the same list:
{#[[1]], 2 #[[2]]} & /@ list1
{{1, 4}, {2, 8}}
Adding the second element from a different list:
list2 = {{a, b}, {c, d}};
{#[[1, 1]], #[[1, 2]] + #[[2, 2]]} & /@ Transpose[{list1, list2}]
{{1, 2 + b}, {2, 4 + d}}
answered 6 hours ago
MelaGoMelaGo
3,1461 gold badge2 silver badges10 bronze badges
3,1461 gold badge2 silver badges10 bronze badges
add a comment
|
add a comment
|
Jim is a new contributor. Be nice, and check out our Code of Conduct.
Jim is a new contributor. Be nice, and check out our Code of Conduct.
Jim is a new contributor. Be nice, and check out our Code of Conduct.
Jim is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Mathematica Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f207289%2fhow-to-add-just-the-second-elements-in-lists-of-pairs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown