ls Ordering[Ordering[list]] optimal? The Next CEO of Stack OverflowOrdering problemHelp with PermutationsSort strings by natural orderingOrdering function with recognition of duplicatesNon-canonical re-ordering of nested listsAn efficient way to merge a pair of 4d arrays of non regular length listsMax element of a list with a custom ordering functionFinding pairs where the intersection of them is empty set from a nested listLexicographic ordering of lists-of-lists?Ordering elements of a list
Visit to the USA with ESTA approved before trip to Iran
Return the Closest Prime Number
Solution of this Diophantine Equation
Is there a good way to store credentials outside of a password manager?
Implement the Thanos sorting algorithm
How to be diplomatic in refusing to write code that breaches the privacy of our users
Grabbing quick drinks
WOW air has ceased operation, can I get my tickets refunded?
What size rim is OK?
What is the difference between "behavior" and "behaviour"?
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
How to start emacs in "nothing" mode (`fundamental-mode`)
How to write the block matrix in LaTex?
Trouble understanding the speech of overseas colleagues
% symbol leads to superlong (forever?) compilations
What is the purpose of the Evocation wizard's Potent Cantrip feature?
Why do remote companies require working in the US?
Explicit solution of a Hamiltonian system
How can I open an app using Terminal?
Inappropriate reference requests from Journal reviewers
What can we do to stop prior company from asking us questions?
Are there languages with no euphemisms?
What's the point of interval inversion?
How should I support this large drywall patch?
ls Ordering[Ordering[list]] optimal?
The Next CEO of Stack OverflowOrdering problemHelp with PermutationsSort strings by natural orderingOrdering function with recognition of duplicatesNon-canonical re-ordering of nested listsAn efficient way to merge a pair of 4d arrays of non regular length listsMax element of a list with a custom ordering functionFinding pairs where the intersection of them is empty set from a nested listLexicographic ordering of lists-of-lists?Ordering elements of a list
$begingroup$
Given a list list with unique elements, the task is to replace each element by its position in Sort[list]. For example,
list = "A", "B", "D", "C", "Z", "W";
Position[Sort[list], #][[1, 1]] & /@ list
1, 2, 4, 3, 6, 5
Much more efficient is to call Ordering twice:
Ordering[Ordering[list]]
1, 2, 4, 3, 6, 5
When applied on a permutation of Range[length] this operation does nothing:
list = 2, 10, 1, 4, 8, 6, 3, 9, 5, 7;
Ordering[Ordering[list]]
2, 10, 1, 4, 8, 6, 3, 9, 5, 7
Question: is there a more efficient way of doing this operation, making a single function call instead of calling Ordering twice?
benchmarks
Solutions are given from fastest to slowest:
L = RandomReal[0, 1, 10^7];
(* Henrik Schumacher *)
R1[[Ordering[L]]] = R1 = Range[Length[L]]; //AbsoluteTiming//First
(* 2.45501 *)
(* orignal post *)
R2 = Ordering[Ordering[L]]; //AbsoluteTiming//First
(* 4.2129 *)
(* J.M. *)
R3 = PermutationList[InversePermutation[FindPermutation[L]]]; //AbsoluteTiming//First
(* 4.75192 *)
(* check *)
R1 == R2 == R3
(* True *)
list-manipulation performance-tuning sorting permutation
$endgroup$
add a comment |
$begingroup$
Given a list list with unique elements, the task is to replace each element by its position in Sort[list]. For example,
list = "A", "B", "D", "C", "Z", "W";
Position[Sort[list], #][[1, 1]] & /@ list
1, 2, 4, 3, 6, 5
Much more efficient is to call Ordering twice:
Ordering[Ordering[list]]
1, 2, 4, 3, 6, 5
When applied on a permutation of Range[length] this operation does nothing:
list = 2, 10, 1, 4, 8, 6, 3, 9, 5, 7;
Ordering[Ordering[list]]
2, 10, 1, 4, 8, 6, 3, 9, 5, 7
Question: is there a more efficient way of doing this operation, making a single function call instead of calling Ordering twice?
benchmarks
Solutions are given from fastest to slowest:
L = RandomReal[0, 1, 10^7];
(* Henrik Schumacher *)
R1[[Ordering[L]]] = R1 = Range[Length[L]]; //AbsoluteTiming//First
(* 2.45501 *)
(* orignal post *)
R2 = Ordering[Ordering[L]]; //AbsoluteTiming//First
(* 4.2129 *)
(* J.M. *)
R3 = PermutationList[InversePermutation[FindPermutation[L]]]; //AbsoluteTiming//First
(* 4.75192 *)
(* check *)
R1 == R2 == R3
(* True *)
list-manipulation performance-tuning sorting permutation
$endgroup$
2
$begingroup$
Have you tried outPermutationList[FindPermutation[list]]orInversePermutation[Ordering[list]]?
$endgroup$
– J. M. is slightly pensive♦
11 hours ago
add a comment |
$begingroup$
Given a list list with unique elements, the task is to replace each element by its position in Sort[list]. For example,
list = "A", "B", "D", "C", "Z", "W";
Position[Sort[list], #][[1, 1]] & /@ list
1, 2, 4, 3, 6, 5
Much more efficient is to call Ordering twice:
Ordering[Ordering[list]]
1, 2, 4, 3, 6, 5
When applied on a permutation of Range[length] this operation does nothing:
list = 2, 10, 1, 4, 8, 6, 3, 9, 5, 7;
Ordering[Ordering[list]]
2, 10, 1, 4, 8, 6, 3, 9, 5, 7
Question: is there a more efficient way of doing this operation, making a single function call instead of calling Ordering twice?
benchmarks
Solutions are given from fastest to slowest:
L = RandomReal[0, 1, 10^7];
(* Henrik Schumacher *)
R1[[Ordering[L]]] = R1 = Range[Length[L]]; //AbsoluteTiming//First
(* 2.45501 *)
(* orignal post *)
R2 = Ordering[Ordering[L]]; //AbsoluteTiming//First
(* 4.2129 *)
(* J.M. *)
R3 = PermutationList[InversePermutation[FindPermutation[L]]]; //AbsoluteTiming//First
(* 4.75192 *)
(* check *)
R1 == R2 == R3
(* True *)
list-manipulation performance-tuning sorting permutation
$endgroup$
Given a list list with unique elements, the task is to replace each element by its position in Sort[list]. For example,
list = "A", "B", "D", "C", "Z", "W";
Position[Sort[list], #][[1, 1]] & /@ list
1, 2, 4, 3, 6, 5
Much more efficient is to call Ordering twice:
Ordering[Ordering[list]]
1, 2, 4, 3, 6, 5
When applied on a permutation of Range[length] this operation does nothing:
list = 2, 10, 1, 4, 8, 6, 3, 9, 5, 7;
Ordering[Ordering[list]]
2, 10, 1, 4, 8, 6, 3, 9, 5, 7
Question: is there a more efficient way of doing this operation, making a single function call instead of calling Ordering twice?
benchmarks
Solutions are given from fastest to slowest:
L = RandomReal[0, 1, 10^7];
(* Henrik Schumacher *)
R1[[Ordering[L]]] = R1 = Range[Length[L]]; //AbsoluteTiming//First
(* 2.45501 *)
(* orignal post *)
R2 = Ordering[Ordering[L]]; //AbsoluteTiming//First
(* 4.2129 *)
(* J.M. *)
R3 = PermutationList[InversePermutation[FindPermutation[L]]]; //AbsoluteTiming//First
(* 4.75192 *)
(* check *)
R1 == R2 == R3
(* True *)
list-manipulation performance-tuning sorting permutation
list-manipulation performance-tuning sorting permutation
edited 10 hours ago
Roman
asked 12 hours ago
RomanRoman
3,8501020
3,8501020
2
$begingroup$
Have you tried outPermutationList[FindPermutation[list]]orInversePermutation[Ordering[list]]?
$endgroup$
– J. M. is slightly pensive♦
11 hours ago
add a comment |
2
$begingroup$
Have you tried outPermutationList[FindPermutation[list]]orInversePermutation[Ordering[list]]?
$endgroup$
– J. M. is slightly pensive♦
11 hours ago
2
2
$begingroup$
Have you tried out
PermutationList[FindPermutation[list]] or InversePermutation[Ordering[list]]?$endgroup$
– J. M. is slightly pensive♦
11 hours ago
$begingroup$
Have you tried out
PermutationList[FindPermutation[list]] or InversePermutation[Ordering[list]]?$endgroup$
– J. M. is slightly pensive♦
11 hours ago
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method:
list = RandomReal[-1, 1, 1000000];
First@RepeatedTiming[
a[[Ordering[list]]] = a = Range[Length[list]];
]
First@RepeatedTiming[
b = Ordering[Ordering[list]]
]
a == b
0.13
0.236
True
$endgroup$
$begingroup$
This solution cuts the effort down from two function calls to one function call plus one indexed substitution (very fast). 90% of what I was looking for, thanks!
$endgroup$
– Roman
10 hours ago
$begingroup$
You're welcome.
$endgroup$
– Henrik Schumacher
10 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
);
);
, "mathjax-editing");
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/3.0/"u003ecc by-sa 3.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%2f194094%2fls-orderingorderinglist-optimal%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$
No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method:
list = RandomReal[-1, 1, 1000000];
First@RepeatedTiming[
a[[Ordering[list]]] = a = Range[Length[list]];
]
First@RepeatedTiming[
b = Ordering[Ordering[list]]
]
a == b
0.13
0.236
True
$endgroup$
$begingroup$
This solution cuts the effort down from two function calls to one function call plus one indexed substitution (very fast). 90% of what I was looking for, thanks!
$endgroup$
– Roman
10 hours ago
$begingroup$
You're welcome.
$endgroup$
– Henrik Schumacher
10 hours ago
add a comment |
$begingroup$
No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method:
list = RandomReal[-1, 1, 1000000];
First@RepeatedTiming[
a[[Ordering[list]]] = a = Range[Length[list]];
]
First@RepeatedTiming[
b = Ordering[Ordering[list]]
]
a == b
0.13
0.236
True
$endgroup$
$begingroup$
This solution cuts the effort down from two function calls to one function call plus one indexed substitution (very fast). 90% of what I was looking for, thanks!
$endgroup$
– Roman
10 hours ago
$begingroup$
You're welcome.
$endgroup$
– Henrik Schumacher
10 hours ago
add a comment |
$begingroup$
No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method:
list = RandomReal[-1, 1, 1000000];
First@RepeatedTiming[
a[[Ordering[list]]] = a = Range[Length[list]];
]
First@RepeatedTiming[
b = Ordering[Ordering[list]]
]
a == b
0.13
0.236
True
$endgroup$
No, Ordering[Ordering[list]] not optimal. And yes, there is a faster method:
list = RandomReal[-1, 1, 1000000];
First@RepeatedTiming[
a[[Ordering[list]]] = a = Range[Length[list]];
]
First@RepeatedTiming[
b = Ordering[Ordering[list]]
]
a == b
0.13
0.236
True
edited 11 hours ago
answered 12 hours ago
Henrik SchumacherHenrik Schumacher
58.3k580160
58.3k580160
$begingroup$
This solution cuts the effort down from two function calls to one function call plus one indexed substitution (very fast). 90% of what I was looking for, thanks!
$endgroup$
– Roman
10 hours ago
$begingroup$
You're welcome.
$endgroup$
– Henrik Schumacher
10 hours ago
add a comment |
$begingroup$
This solution cuts the effort down from two function calls to one function call plus one indexed substitution (very fast). 90% of what I was looking for, thanks!
$endgroup$
– Roman
10 hours ago
$begingroup$
You're welcome.
$endgroup$
– Henrik Schumacher
10 hours ago
$begingroup$
This solution cuts the effort down from two function calls to one function call plus one indexed substitution (very fast). 90% of what I was looking for, thanks!
$endgroup$
– Roman
10 hours ago
$begingroup$
This solution cuts the effort down from two function calls to one function call plus one indexed substitution (very fast). 90% of what I was looking for, thanks!
$endgroup$
– Roman
10 hours ago
$begingroup$
You're welcome.
$endgroup$
– Henrik Schumacher
10 hours ago
$begingroup$
You're welcome.
$endgroup$
– Henrik Schumacher
10 hours ago
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%2f194094%2fls-orderingorderinglist-optimal%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
2
$begingroup$
Have you tried out
PermutationList[FindPermutation[list]]orInversePermutation[Ordering[list]]?$endgroup$
– J. M. is slightly pensive♦
11 hours ago