ls Ordering[Ordering[list]] optimal?Sort+Union on a listEquivalence under common permutationsMax element of a...

Can someone clarify the logic behind the given equation?

What deity do celestials/aasimars worship?

What is the meaning of the square breakpoint in Visual Studio?

How easy is it to start Magic from scratch?

Describing a person. What needs to be mentioned?

Under what conditions does the function C = f(A,B) satisfy H(C|A) = H(B)?

How did Arya survive the stabbing?

What happens if you roll doubles 3 times then land on "Go to jail?"

Was a professor correct to chastise me for writing "Prof. X" rather than "Professor X"?

Failed to fetch jessie backports repository

What is the difference between "behavior" and "behaviour"?

Tiptoe or tiphoof? Adjusting words to better fit fantasy races

Is expanding the research of a group into machine learning as a PhD student risky?

Is the destination of a commercial flight important for the pilot?

Is HostGator storing my password in plaintext?

How to check is there any negative term in a large list?

Lay out the Carpet

Do sorcerers' Subtle Spells require a skill check to be unseen?

Return the Closest Prime Number

What grammatical function is や performing here?

What can we do to stop prior company from asking us questions?

What does 算不上 mean in 算不上太美好的日子?

How do I go from 300 unfinished/half written blog posts, to published posts?

Abbreviate author names as "Lastname AB" (without space or period) in bibliography



ls Ordering[Ordering[list]] optimal?


Sort+Union on a listEquivalence under common permutationsMax element of a list with a custom ordering functionReplacing members of a list with elements sampled without replacement from another listFinding pairs where the intersection of them is empty set from a nested listLength of Union Incorrect?Changing the order of elements in a listHow to efficiently implement Disjoint Set/Union Find data structure?Fast find sublist in a list procedure (for packed arrays of integers)Finding $n^text{th}$ largest/smallest element













4












$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 *)









share|improve this question











$endgroup$








  • 1




    $begingroup$
    Have you tried out PermutationList[FindPermutation[list]] or InversePermutation[Ordering[list]]?
    $endgroup$
    – J. M. is slightly pensive
    49 mins ago


















4












$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 *)









share|improve this question











$endgroup$








  • 1




    $begingroup$
    Have you tried out PermutationList[FindPermutation[list]] or InversePermutation[Ordering[list]]?
    $endgroup$
    – J. M. is slightly pensive
    49 mins ago
















4












4








4


1



$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 *)









share|improve this question











$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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 4 mins ago







Roman

















asked 1 hour ago









RomanRoman

3,7951020




3,7951020








  • 1




    $begingroup$
    Have you tried out PermutationList[FindPermutation[list]] or InversePermutation[Ordering[list]]?
    $endgroup$
    – J. M. is slightly pensive
    49 mins ago
















  • 1




    $begingroup$
    Have you tried out PermutationList[FindPermutation[list]] or InversePermutation[Ordering[list]]?
    $endgroup$
    – J. M. is slightly pensive
    49 mins ago










1




1




$begingroup$
Have you tried out PermutationList[FindPermutation[list]] or InversePermutation[Ordering[list]]?
$endgroup$
– J. M. is slightly pensive
49 mins ago






$begingroup$
Have you tried out PermutationList[FindPermutation[list]] or InversePermutation[Ordering[list]]?
$endgroup$
– J. M. is slightly pensive
49 mins ago












1 Answer
1






active

oldest

votes


















3












$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







share|improve this answer











$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
    2 mins ago











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
});


}
});














draft saved

draft discarded


















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









3












$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







share|improve this answer











$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
    2 mins ago
















3












$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







share|improve this answer











$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
    2 mins ago














3












3








3





$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







share|improve this answer











$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








share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 1 hour ago









Henrik SchumacherHenrik Schumacher

58.1k580160




58.1k580160












  • $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
    2 mins 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
    2 mins 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
2 mins 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
2 mins ago


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Fairchild Swearingen Metro Inhaltsverzeichnis Geschichte | Innenausstattung | Nutzung | Zwischenfälle...

Pilgersdorf Inhaltsverzeichnis Geografie | Geschichte | Bevölkerungsentwicklung | Politik | Kultur...

Marineschifffahrtleitung Inhaltsverzeichnis Geschichte | Heutige Organisation der NATO | Nationale und...