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










9












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







  • 2




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
















9












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







  • 2




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














9












9








9


2



$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 10 hours ago







Roman

















asked 12 hours ago









RomanRoman

3,8501020




3,8501020







  • 2




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













  • 2




    $begingroup$
    Have you tried out PermutationList[FindPermutation[list]] or InversePermutation[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











1 Answer
1






active

oldest

votes


















9












$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
    10 hours ago










  • $begingroup$
    You're welcome.
    $endgroup$
    – Henrik Schumacher
    10 hours 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









9












$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
    10 hours ago










  • $begingroup$
    You're welcome.
    $endgroup$
    – Henrik Schumacher
    10 hours ago















9












$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
    10 hours ago










  • $begingroup$
    You're welcome.
    $endgroup$
    – Henrik Schumacher
    10 hours ago













9












9








9





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
















  • $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

















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

Tórshavn Kliima | Partnerstääden | Luke uk diar | Nawigatsjuun62° 1′ N, 6° 46′ W62° 1′ 0″ N, 6° 46′ 0″ WWMOTórshavn

南部首創開放式體驗廚房 用智慧廚具做出好料理

大學姐愛和弟洗澡 突襲浴室求共浴