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 8852419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:23:46+00:00 2026-06-14T13:23:46+00:00

In Matlab, sort returns both the sorted vector and an index vector showing which

  • 0

In Matlab, sort returns both the sorted vector and an index vector showing which vector element has been moved where:

[v, ix] = sort(u);

Here, v is a vector containing all the elements of u, but sorted. ix is a vector showing the original position of each element of v in u. Using Matlab’s syntax, u(ix) == v.

My question: How do I obtain u from v and ix?

Of course, I could simply use:

w = zero(size(v));

for i = 1:length(v)
    w(ix(i)) = v(i)
end

if nnz(w == u) == length(u)
    print('Success!');
else
    print('Failed!');
end

But I am having this tip-of-the-tongue feeling that there is a more elegant, single-statement, vectorized way of doing this.


If you are wondering why one would need to do this instead of just using u: I was trying to implement the Benjamini-Hochberg procedure which adjusts each element of the vector based on its position after sorting, but recovering the original order after adjusting was important for me.

  • 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-06-14T13:23:48+00:00Added an answer on June 14, 2026 at 1:23 pm

    The solution is:

    w(ix) = v;
    

    This is a valid Matlab operation provided that w is either at least as big as v, or not yet declared.

    Example:

    >> u = [4 8 10 6 2];
    >> [v, ix] = sort(u)
    
        v = 2 4 6 8 10        
        ix = 5 1 4 2 3
    
    >> u(ix)
    
        ans = 2 4 6 8 10
    
    >> w(ix) = v
    
        w = 4 8 10 6 2
    

    (Apologies for the trivial question-answer, but I realized the solution as I was typing the question, and thought it might be useful to someone.)

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

Sidebar

Related Questions

My matlab code does image processing, and I made matlab function which has 2
I have never used Python before, most of my programming has been in MATLAB
I'm working on a Matlab GUI application that has multiple views. It behaves sort
Folks, Matlab 2007b (7.5.0) has an avgpower function. See here : The avgpower method
Matlab returns values as 1.0e+04 * [matrix here] some of the values inside the
MATLAB has a very convenient syntax for getting half of a list: x(1:end/2) The
Hey all. Im trying to sort out how to get MATLAB running as best
I need to create a queue in matlab that holds structs which are very
I have a 3D Matrix M(256x256x136) and each index(i,j,k) in M has a gray
I am using the command [A,idx] = sort(A) , which sorts the matrix A

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.