Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3616214
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:28:19+00:00 2026-05-18T22:28:19+00:00

Suppose I have a function f that takes a vector v and returns a

  • 0

Suppose I have a function f that takes a vector v and returns a new vector with the elements transformed in some way.
It does that by calling function g that assumes the vector is sorted.
So I want f to be defined like so:

f[v_] := Module[{s, r},
  s = Sort[v];  (* remember the permutation applied in order to sort v *)
  r = g[s];
  Unsort[r]     (* apply the inverse of that permutation *)
]

What’s the best way to do the “Unsort”?

Or could we get really fancy and have this somehow work:

answer = Unsort[g[Sort[v]]];

ADDED: Let’s make this concrete with a toy example.
Suppose we want a function f that takes a vector and transforms it by adding to each element the next smallest element, if any.
That’s easy to write if we assume the vector is sorted, so let’s write a helper function g that makes that assumption:

g[v_] := v + Prepend[Most@v, 0]

Now for the function we really want, f, that works whether or not v is sorted:

f[v_] := (* remember the order; 
            sort it;
            call g on it;
            put it back in the original order;
            return it
         *)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:28:20+00:00Added an answer on May 18, 2026 at 10:28 pm

    Thanks to TomD and Yaroslav, here’s probably the most concise/elegant way to do it:

    f[v_] := g[Sort@v][[Ordering@Ordering@v]]
    

    And thanks to Janus, here’s a perhaps more efficient way:

    f[v_] := With[{o = Ordering@v}, g[v[[o]]][[Ordering@o]]]
    

    Note that it does 2 sorts instead of 3.

    For posterity, here’s my original attempt, though I don’t think it has anything to recommend it over the above:

    f[v_] := With[{o = Ordering[v]}, Sort[Transpose[{o,g[v[[o]]]}]][[All,-1]]]
    

    To address belisarius in the comments, the reason I’m not passing g as a parameter is because I’m thinking of g as a helper function for f.
    It’s like I have a function f that would be easier to write if I could assume its argument was a sorted vector.
    So I write the version that assumes that and then do this wrapper trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
Suppose I have a function/method F() that takes 3 parameters $A, $B and $C
I have C function that takes string pointer as a parameter. This functions returns
Suppose I have a function def app2(environ, start_response) If I know that a server
Suppose I have a function which takes a nullary functor as an argument: void
I have a function I've written that was initially supposed to take a string
Suppose I have the following code: class siteMS { ... function __CONSTRUCT() { require
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
I have this example code that doesn't compile: #include <iostream> #include <vector> using std::endl;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.