Mapping a function f[xi_,xj_] over a list x1, …, xn with the i < j restrictionSelectively Mapping over elements in a ListWhy is MapIndexed better than mapping over a range?Mapping a function that assigns values to a list over a different listfunction over list with conditionMapping slots over listsMapping over a piecewise functionMapping a function with levelspec vs. Mapping over a flattened list
Subassembly identification
Can I exit and reenter a UK station while waiting for a connecting train?
Was Switzerland pressured either by Allies or Axis to take part in World War 2 at any time?
Was Hitler exclaiming "Heil Hitler!" himself when saluting?
Automatically creating table with borders
Why can a T* be passed in register, but a unique_ptr<T> cannot?
Convexity of a QP
What is the white stuff emerging from a hole on the surface of Mars?
QGIS 3.6.1 problems with CRS
Modeling the Round (Nearest Integer) function
How do social media apps notify you when someone else takes a screenshot of your profile?
If equal temperament divides octave into 12 equal parts, why hertz differences are not the same but element 12th of two?
For piano scales, should I let go of the previous key before I hit the next one?
Would fantasy dwarves be able to invent and/or manufacture the radio?
What does "he was equally game to slip into bit parts" mean?
Vertical Yagi spun at 1000 RPM: gain properties?
Should I respond to a sabotage accusation e-mail at work?
Are there any Baryons that have quark-antiquark combinations?
In this scene from the novel, 'The Martian', by Andy Weir, how does Mark Watney store hydrogen made from water in the tank?
Why doesn't the nucleus have "nucleus-probability cloud"?
Why not 1.d4 Nf6 2.d5?
Options for passes to national parks in Arizona/Utah for 5 people travelling in one car
RAM stress test
Why didn't Aboriginal Australians discover agriculture?
Mapping a function f[xi_,xj_] over a list x1, …, xn with the i
Selectively Mapping over elements in a ListWhy is MapIndexed better than mapping over a range?Mapping a function that assigns values to a list over a different listfunction over list with conditionMapping slots over listsMapping over a piecewise functionMapping a function with levelspec vs. Mapping over a flattened list
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;
.everyonelovesstackoverflowposition:absolute;height:1px;width:1px;opacity:0;top:0;left:0;pointer-events:none;
$begingroup$
I am often facing the situation where I have a list $...x_k...$ and where I want to compute:
$$
...f[x_i,x_j]...text with i<j
$$
Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):
n = 4;
x = Table[RandomReal[], n]
0.547984, 0.601967, 0.000917483, 0.738287
Map[f[Apply[Sequence, x[[#]]]] &,
Flatten[Table[i, j, i, 1, n, j, i + 1, n], 1]]
f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]
(in real situation f is defined f[xi_,xj_]:= xi-xj for instance)
Question: what is a better (builtin?) solution?
list-manipulation programming
$endgroup$
add a comment
|
$begingroup$
I am often facing the situation where I have a list $...x_k...$ and where I want to compute:
$$
...f[x_i,x_j]...text with i<j
$$
Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):
n = 4;
x = Table[RandomReal[], n]
0.547984, 0.601967, 0.000917483, 0.738287
Map[f[Apply[Sequence, x[[#]]]] &,
Flatten[Table[i, j, i, 1, n, j, i + 1, n], 1]]
f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]
(in real situation f is defined f[xi_,xj_]:= xi-xj for instance)
Question: what is a better (builtin?) solution?
list-manipulation programming
$endgroup$
add a comment
|
$begingroup$
I am often facing the situation where I have a list $...x_k...$ and where I want to compute:
$$
...f[x_i,x_j]...text with i<j
$$
Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):
n = 4;
x = Table[RandomReal[], n]
0.547984, 0.601967, 0.000917483, 0.738287
Map[f[Apply[Sequence, x[[#]]]] &,
Flatten[Table[i, j, i, 1, n, j, i + 1, n], 1]]
f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]
(in real situation f is defined f[xi_,xj_]:= xi-xj for instance)
Question: what is a better (builtin?) solution?
list-manipulation programming
$endgroup$
I am often facing the situation where I have a list $...x_k...$ and where I want to compute:
$$
...f[x_i,x_j]...text with i<j
$$
Unfortunately, I have not been able to find an elegant solution for this problem. What I'm doing right now seems horrible to me (indirections etc...):
n = 4;
x = Table[RandomReal[], n]
0.547984, 0.601967, 0.000917483, 0.738287
Map[f[Apply[Sequence, x[[#]]]] &,
Flatten[Table[i, j, i, 1, n, j, i + 1, n], 1]]
f[0.547984, 0.601967], f[0.547984, 0.000917483], f[0.547984,
0.738287], f[0.601967, 0.000917483], f[0.601967, 0.738287], f[0.000917483, 0.738287]
(in real situation f is defined f[xi_,xj_]:= xi-xj for instance)
Question: what is a better (builtin?) solution?
list-manipulation programming
list-manipulation programming
edited May 28 at 11:40
Picaud Vincent
asked May 28 at 11:14
Picaud VincentPicaud Vincent
1,5827 silver badges18 bronze badges
1,5827 silver badges18 bronze badges
add a comment
|
add a comment
|
1 Answer
1
active
oldest
votes
$begingroup$
Subsets[f @@ x, 2]
f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]
You can also use
f @@@ Subsets[x, 2]
Join @@ Table[f @@ x[[i, j]], i, 1, n, j, i + 1, n]
$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
);
);
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%2f199243%2fmapping-a-function-fxi-xj-over-a-list-x1-xn-with-the-i-j-restrict%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Subsets[f @@ x, 2]
f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]
You can also use
f @@@ Subsets[x, 2]
Join @@ Table[f @@ x[[i, j]], i, 1, n, j, i + 1, n]
$endgroup$
add a comment
|
$begingroup$
Subsets[f @@ x, 2]
f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]
You can also use
f @@@ Subsets[x, 2]
Join @@ Table[f @@ x[[i, j]], i, 1, n, j, i + 1, n]
$endgroup$
add a comment
|
$begingroup$
Subsets[f @@ x, 2]
f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]
You can also use
f @@@ Subsets[x, 2]
Join @@ Table[f @@ x[[i, j]], i, 1, n, j, i + 1, n]
$endgroup$
Subsets[f @@ x, 2]
f[0.817389, 0.11142], f[0.817389, 0.789526], f[0.817389, 0.187803],
f[0.11142, 0.789526], f[0.11142, 0.187803], f[0.789526, 0.187803]
You can also use
f @@@ Subsets[x, 2]
Join @@ Table[f @@ x[[i, j]], i, 1, n, j, i + 1, n]
edited May 28 at 11:37
answered May 28 at 11:32
kglrkglr
221k10 gold badges251 silver badges509 bronze badges
221k10 gold badges251 silver badges509 bronze badges
add a comment
|
add a comment
|
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%2f199243%2fmapping-a-function-fxi-xj-over-a-list-x1-xn-with-the-i-j-restrict%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