Terse Method to Swap Lowest for Highest?Efficient method for Inserting arrays into arraysSwap elements in list without copyBetter method to swap the values of two 2-D arraysHow to get this list with a terse methodBuilt-in (or Terse) Method to Combine and Transpose DatasetsAre there more readable and terse method can get this listefficiently method for generating a sequenceSimple method to sort versionsFunction for SortBySwap Elements of a continuous List, possible?
Visiting the UK as unmarried couple
My boss asked me to take a one-day class, then signs it up as a day off
Are Warlocks Arcane or Divine?
Reply ‘no position’ while the job posting is still there (‘HiWi’ position in Germany)
How to check participants in at events?
For airliners, what prevents wing strikes on landing in bad weather?
Is there enough fresh water in the world to eradicate the drinking water crisis?
Simple recursive Sudoku solver
How to prevent YouTube from showing already watched videos?
Hostile work environment after whistle-blowing on coworker and our boss. What do I do?
Is there an Impartial Brexit Deal comparison site?
Is there an wasy way to program in Tikz something like the one in the image?
Greatest common substring
A known event to a history junkie
Can I Retrieve Email Addresses from BCC?
Lightning Web Component - do I need to track changes for every single input field in a form
Freedom of speech and where it applies
How can a jailer prevent the Forge Cleric's Artisan's Blessing from being used?
What to do when my ideas aren't chosen, when I strongly disagree with the chosen solution?
Are taller landing gear bad for aircraft, particulary large airliners?
Why Were Madagascar and New Zealand Discovered So Late?
What was required to accept "troll"?
Giant Toughroad SLR 2 for 200 miles in two days, will it make it?
Organic chemistry Iodoform Reaction
Terse Method to Swap Lowest for Highest?
Efficient method for Inserting arrays into arraysSwap elements in list without copyBetter method to swap the values of two 2-D arraysHow to get this list with a terse methodBuilt-in (or Terse) Method to Combine and Transpose DatasetsAre there more readable and terse method can get this listefficiently method for generating a sequenceSimple method to sort versionsFunction for SortBySwap Elements of a continuous List, possible?
$begingroup$
I have built a solution to swap the lowest values with the highest values in a list.
With
SeedRandom[987]
test = RandomSample@*Join @@ Range @@@ 6, 10, 56, 60, 1, 5, -5, -1
-1, 2, 7, 8, 60, 57, 58, 10, 9, 4, -5, -3, 3, 59, 1, 5, -4, 6, -2, 56
Then
swapPositions =
PermutationReplace[
Ordering@Ordering@test,
With[len = Length@test,
Cycles@
Transpose@Range @@ 1, Floor[len/2], Reverse@*Range @@ Ceiling[len/2] + 1, len
]
];
Sort[test][[swapPositions]]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
The largest half of the numbers have had their positions swapped with lowest half of the numbers.
However, it feels too verbose and I think Sort
might be expensive in this case. Is there a built-in function or more terse method to achieve this. Of course with no loss in speed. The actual case is for list of length 100000 and more.
list-manipulation performance-tuning sorting permutation
$endgroup$
add a comment |
$begingroup$
I have built a solution to swap the lowest values with the highest values in a list.
With
SeedRandom[987]
test = RandomSample@*Join @@ Range @@@ 6, 10, 56, 60, 1, 5, -5, -1
-1, 2, 7, 8, 60, 57, 58, 10, 9, 4, -5, -3, 3, 59, 1, 5, -4, 6, -2, 56
Then
swapPositions =
PermutationReplace[
Ordering@Ordering@test,
With[len = Length@test,
Cycles@
Transpose@Range @@ 1, Floor[len/2], Reverse@*Range @@ Ceiling[len/2] + 1, len
]
];
Sort[test][[swapPositions]]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
The largest half of the numbers have had their positions swapped with lowest half of the numbers.
However, it feels too verbose and I think Sort
might be expensive in this case. Is there a built-in function or more terse method to achieve this. Of course with no loss in speed. The actual case is for list of length 100000 and more.
list-manipulation performance-tuning sorting permutation
$endgroup$
add a comment |
$begingroup$
I have built a solution to swap the lowest values with the highest values in a list.
With
SeedRandom[987]
test = RandomSample@*Join @@ Range @@@ 6, 10, 56, 60, 1, 5, -5, -1
-1, 2, 7, 8, 60, 57, 58, 10, 9, 4, -5, -3, 3, 59, 1, 5, -4, 6, -2, 56
Then
swapPositions =
PermutationReplace[
Ordering@Ordering@test,
With[len = Length@test,
Cycles@
Transpose@Range @@ 1, Floor[len/2], Reverse@*Range @@ Ceiling[len/2] + 1, len
]
];
Sort[test][[swapPositions]]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
The largest half of the numbers have had their positions swapped with lowest half of the numbers.
However, it feels too verbose and I think Sort
might be expensive in this case. Is there a built-in function or more terse method to achieve this. Of course with no loss in speed. The actual case is for list of length 100000 and more.
list-manipulation performance-tuning sorting permutation
$endgroup$
I have built a solution to swap the lowest values with the highest values in a list.
With
SeedRandom[987]
test = RandomSample@*Join @@ Range @@@ 6, 10, 56, 60, 1, 5, -5, -1
-1, 2, 7, 8, 60, 57, 58, 10, 9, 4, -5, -3, 3, 59, 1, 5, -4, 6, -2, 56
Then
swapPositions =
PermutationReplace[
Ordering@Ordering@test,
With[len = Length@test,
Cycles@
Transpose@Range @@ 1, Floor[len/2], Reverse@*Range @@ Ceiling[len/2] + 1, len
]
];
Sort[test][[swapPositions]]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
The largest half of the numbers have had their positions swapped with lowest half of the numbers.
However, it feels too verbose and I think Sort
might be expensive in this case. Is there a built-in function or more terse method to achieve this. Of course with no loss in speed. The actual case is for list of length 100000 and more.
list-manipulation performance-tuning sorting permutation
list-manipulation performance-tuning sorting permutation
edited Mar 23 at 2:15
J. M. is slightly pensive♦
98.5k10308466
98.5k10308466
asked Mar 22 at 20:57
EdmundEdmund
26.7k330103
26.7k330103
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
How about:
Module[tmp = test,
With[ord=Ordering[tmp],
tmp[[ord]] = Reverse @ tmp[[ord]]];
tmp
]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
$endgroup$
1
$begingroup$
That is so obvious I want to cry. Thanks (+1).
$endgroup$
– Edmund
Mar 22 at 21:15
add a comment |
$begingroup$
This is equivalent to Carl's procedure, except that it uses one less scratch list:
With[ord = Ordering[test],
test[[PermutationProduct[Reverse[ord], InversePermutation[ord]]]]]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
Recall that list[[perm]] = list
is equivalent to list = list[[InversePermutation[perm]]]
, where perm
is a permutation list. (The situation is equivalent to list.pmat
being the same as Transpose[pmat].list
if pmat
is a permutation matrix.) You can then use PermutationProduct[]
to compose successive permutations.
(This was supposed to be a comment, but it got too long.)
$endgroup$
$begingroup$
This solution doesn't copy the list so may be faster than Carl's. (+1).
$endgroup$
– Edmund
Mar 23 at 3:38
$begingroup$
FWIW, I consistently get 56, -2, 6, -4, 5, 1, 59, 3, -3, -5, 4, 9, 10, 58, 57, 60, 8, 7, 2, -1 from this.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, what version number of Mathematica is giving that result?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
$begingroup$
11.3.0.0 (5944644, 2018030701) Win 10. I did a trace, which might have had the needed info but I didn't catch it. Started w/ fresh kernel, & repeated, w/ same result. Baffled.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, can you try withWith[ord = Ordering[test], test[[PermutationProduct[InversePermutation[ord], Reverse[ord]]]]]
?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
|
show 2 more comments
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%2f193790%2fterse-method-to-swap-lowest-for-highest%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
How about:
Module[tmp = test,
With[ord=Ordering[tmp],
tmp[[ord]] = Reverse @ tmp[[ord]]];
tmp
]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
$endgroup$
1
$begingroup$
That is so obvious I want to cry. Thanks (+1).
$endgroup$
– Edmund
Mar 22 at 21:15
add a comment |
$begingroup$
How about:
Module[tmp = test,
With[ord=Ordering[tmp],
tmp[[ord]] = Reverse @ tmp[[ord]]];
tmp
]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
$endgroup$
1
$begingroup$
That is so obvious I want to cry. Thanks (+1).
$endgroup$
– Edmund
Mar 22 at 21:15
add a comment |
$begingroup$
How about:
Module[tmp = test,
With[ord=Ordering[tmp],
tmp[[ord]] = Reverse @ tmp[[ord]]];
tmp
]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
$endgroup$
How about:
Module[tmp = test,
With[ord=Ordering[tmp],
tmp[[ord]] = Reverse @ tmp[[ord]]];
tmp
]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
answered Mar 22 at 21:11
Carl WollCarl Woll
71.6k394186
71.6k394186
1
$begingroup$
That is so obvious I want to cry. Thanks (+1).
$endgroup$
– Edmund
Mar 22 at 21:15
add a comment |
1
$begingroup$
That is so obvious I want to cry. Thanks (+1).
$endgroup$
– Edmund
Mar 22 at 21:15
1
1
$begingroup$
That is so obvious I want to cry. Thanks (+1).
$endgroup$
– Edmund
Mar 22 at 21:15
$begingroup$
That is so obvious I want to cry. Thanks (+1).
$endgroup$
– Edmund
Mar 22 at 21:15
add a comment |
$begingroup$
This is equivalent to Carl's procedure, except that it uses one less scratch list:
With[ord = Ordering[test],
test[[PermutationProduct[Reverse[ord], InversePermutation[ord]]]]]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
Recall that list[[perm]] = list
is equivalent to list = list[[InversePermutation[perm]]]
, where perm
is a permutation list. (The situation is equivalent to list.pmat
being the same as Transpose[pmat].list
if pmat
is a permutation matrix.) You can then use PermutationProduct[]
to compose successive permutations.
(This was supposed to be a comment, but it got too long.)
$endgroup$
$begingroup$
This solution doesn't copy the list so may be faster than Carl's. (+1).
$endgroup$
– Edmund
Mar 23 at 3:38
$begingroup$
FWIW, I consistently get 56, -2, 6, -4, 5, 1, 59, 3, -3, -5, 4, 9, 10, 58, 57, 60, 8, 7, 2, -1 from this.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, what version number of Mathematica is giving that result?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
$begingroup$
11.3.0.0 (5944644, 2018030701) Win 10. I did a trace, which might have had the needed info but I didn't catch it. Started w/ fresh kernel, & repeated, w/ same result. Baffled.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, can you try withWith[ord = Ordering[test], test[[PermutationProduct[InversePermutation[ord], Reverse[ord]]]]]
?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
|
show 2 more comments
$begingroup$
This is equivalent to Carl's procedure, except that it uses one less scratch list:
With[ord = Ordering[test],
test[[PermutationProduct[Reverse[ord], InversePermutation[ord]]]]]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
Recall that list[[perm]] = list
is equivalent to list = list[[InversePermutation[perm]]]
, where perm
is a permutation list. (The situation is equivalent to list.pmat
being the same as Transpose[pmat].list
if pmat
is a permutation matrix.) You can then use PermutationProduct[]
to compose successive permutations.
(This was supposed to be a comment, but it got too long.)
$endgroup$
$begingroup$
This solution doesn't copy the list so may be faster than Carl's. (+1).
$endgroup$
– Edmund
Mar 23 at 3:38
$begingroup$
FWIW, I consistently get 56, -2, 6, -4, 5, 1, 59, 3, -3, -5, 4, 9, 10, 58, 57, 60, 8, 7, 2, -1 from this.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, what version number of Mathematica is giving that result?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
$begingroup$
11.3.0.0 (5944644, 2018030701) Win 10. I did a trace, which might have had the needed info but I didn't catch it. Started w/ fresh kernel, & repeated, w/ same result. Baffled.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, can you try withWith[ord = Ordering[test], test[[PermutationProduct[InversePermutation[ord], Reverse[ord]]]]]
?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
|
show 2 more comments
$begingroup$
This is equivalent to Carl's procedure, except that it uses one less scratch list:
With[ord = Ordering[test],
test[[PermutationProduct[Reverse[ord], InversePermutation[ord]]]]]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
Recall that list[[perm]] = list
is equivalent to list = list[[InversePermutation[perm]]]
, where perm
is a permutation list. (The situation is equivalent to list.pmat
being the same as Transpose[pmat].list
if pmat
is a permutation matrix.) You can then use PermutationProduct[]
to compose successive permutations.
(This was supposed to be a comment, but it got too long.)
$endgroup$
This is equivalent to Carl's procedure, except that it uses one less scratch list:
With[ord = Ordering[test],
test[[PermutationProduct[Reverse[ord], InversePermutation[ord]]]]]
56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1
Recall that list[[perm]] = list
is equivalent to list = list[[InversePermutation[perm]]]
, where perm
is a permutation list. (The situation is equivalent to list.pmat
being the same as Transpose[pmat].list
if pmat
is a permutation matrix.) You can then use PermutationProduct[]
to compose successive permutations.
(This was supposed to be a comment, but it got too long.)
edited Mar 23 at 2:27
answered Mar 23 at 2:14
J. M. is slightly pensive♦J. M. is slightly pensive
98.5k10308466
98.5k10308466
$begingroup$
This solution doesn't copy the list so may be faster than Carl's. (+1).
$endgroup$
– Edmund
Mar 23 at 3:38
$begingroup$
FWIW, I consistently get 56, -2, 6, -4, 5, 1, 59, 3, -3, -5, 4, 9, 10, 58, 57, 60, 8, 7, 2, -1 from this.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, what version number of Mathematica is giving that result?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
$begingroup$
11.3.0.0 (5944644, 2018030701) Win 10. I did a trace, which might have had the needed info but I didn't catch it. Started w/ fresh kernel, & repeated, w/ same result. Baffled.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, can you try withWith[ord = Ordering[test], test[[PermutationProduct[InversePermutation[ord], Reverse[ord]]]]]
?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
|
show 2 more comments
$begingroup$
This solution doesn't copy the list so may be faster than Carl's. (+1).
$endgroup$
– Edmund
Mar 23 at 3:38
$begingroup$
FWIW, I consistently get 56, -2, 6, -4, 5, 1, 59, 3, -3, -5, 4, 9, 10, 58, 57, 60, 8, 7, 2, -1 from this.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, what version number of Mathematica is giving that result?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
$begingroup$
11.3.0.0 (5944644, 2018030701) Win 10. I did a trace, which might have had the needed info but I didn't catch it. Started w/ fresh kernel, & repeated, w/ same result. Baffled.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, can you try withWith[ord = Ordering[test], test[[PermutationProduct[InversePermutation[ord], Reverse[ord]]]]]
?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
$begingroup$
This solution doesn't copy the list so may be faster than Carl's. (+1).
$endgroup$
– Edmund
Mar 23 at 3:38
$begingroup$
This solution doesn't copy the list so may be faster than Carl's. (+1).
$endgroup$
– Edmund
Mar 23 at 3:38
$begingroup$
FWIW, I consistently get 56, -2, 6, -4, 5, 1, 59, 3, -3, -5, 4, 9, 10, 58, 57, 60, 8, 7, 2, -1 from this.
$endgroup$
– Rabbit
2 days ago
$begingroup$
FWIW, I consistently get 56, -2, 6, -4, 5, 1, 59, 3, -3, -5, 4, 9, 10, 58, 57, 60, 8, 7, 2, -1 from this.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, what version number of Mathematica is giving that result?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
$begingroup$
@Rabbit, what version number of Mathematica is giving that result?
$endgroup$
– J. M. is slightly pensive♦
2 days ago
$begingroup$
11.3.0.0 (5944644, 2018030701) Win 10. I did a trace, which might have had the needed info but I didn't catch it. Started w/ fresh kernel, & repeated, w/ same result. Baffled.
$endgroup$
– Rabbit
2 days ago
$begingroup$
11.3.0.0 (5944644, 2018030701) Win 10. I did a trace, which might have had the needed info but I didn't catch it. Started w/ fresh kernel, & repeated, w/ same result. Baffled.
$endgroup$
– Rabbit
2 days ago
$begingroup$
@Rabbit, can you try with
With[ord = Ordering[test], test[[PermutationProduct[InversePermutation[ord], Reverse[ord]]]]]
?$endgroup$
– J. M. is slightly pensive♦
2 days ago
$begingroup$
@Rabbit, can you try with
With[ord = Ordering[test], test[[PermutationProduct[InversePermutation[ord], Reverse[ord]]]]]
?$endgroup$
– J. M. is slightly pensive♦
2 days ago
|
show 2 more comments
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%2f193790%2fterse-method-to-swap-lowest-for-highest%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
-list-manipulation, performance-tuning, permutation, sorting