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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:08:27+00:00 2026-05-26T16:08:27+00:00

I am using the command [A,idx] = sort(A) , which sorts the matrix A

  • 0

I am using the command [A,idx] = sort(A), which sorts the matrix A columnwise, which is what I want. The idx matrix shows how each column of A has been sorted. Now if I try and access B(idx) MATLAB assumes I am referencing B in an absolute manner. This is however not what I want, I want the columns of B sorted with the same scheme A was sorted with.

I am therefore looking for a command that will index a matrix columnwise. Of course I can easily do this with a loop, but it seems like a pretty poor solution. I have also come up with this solution, but it’s not very pretty

idx = idx+repmat(0:size(idx,1):size(idx,2)*size(idx,1)-1,size(idx,1),1)

Thanks in advance.

  • 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-26T16:08:28+00:00Added an answer on May 26, 2026 at 4:08 pm

    Consider the following:

    A = randi([1 10],[5 4]);
    [AA,idx] = sort(A);
    

    Your solution is not bad, you could improve it a bit by replacing the REPMAT call with BSXFUN:

    idx = bsxfun(@plus, idx, 0:size(idx,1):numel(idx)-1);
    isequal(AA,A(idx))
    

    Another alternative is to convert to linear indices using SUB2IND:

    idx = sub2ind(size(idx), idx, repmat(1:size(idx,2),size(idx,1),1));
    isequal(AA,A(idx))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using command line git with Java. Our project has tons of nested packages which
I am using this command: cut -d: -f2 To sort and reedit text, Is
I've built a Maven Grails project which can be build fine using command mvn
I have jar files which I constructed using command jar -cf Generic.jar /java/mypackage/*.class There
I want to delete a row in ListView Control using Command and CommandParameter like
I want to restart Windows service using command prompt in [Icons] section using Inno
Question: How do send data to a view model when using command binding? So
I'm trying to connect from one server to another using FTP. By using command
In Delphi 2009, how can I build a project using command line. I tried
Using sc command we can query, start , stop windows services. For ex: sc

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.