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

  • Home
  • SEARCH
  • 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 4271540
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T07:27:21+00:00 2026-05-21T07:27:21+00:00

Let’s said you have a 1D matrix a = rand(1,5); [sa i] = sort(a);

  • 0

Let’s said you have a 1D matrix

a = rand(1,5);
[sa i] = sort(a);

then sa and a(i) are the same. However, if the size of the matrix increases

a = rand(3,4);
[sa i] = sort(a);

then sa and a(i) are not the same. And the same happens when I’m trying to sort a 3D matrix by its third dimension.

How can I access the values of a through the index i? Or in other words how can I compute the sa=a(X), what X should be?

Edit:

Thanks for the solutions. However, they don’t work when you change the dimension to sort by. Nevertheless, I pick up the idea and use it to build a general form.

What the algorithm is doing is to build the indexes of the matrix. MATLAB index the cells column wise. Therefore, the index is given by

idx = r + (c-1)*ROWS + (p-1)*ROWS*COLS

where, idx is the index, r is the row position, c is the column position, and p is the page position.

Therefore, if we sort in the first dimension (normal sort(a)) the result index is the position in the columns; if we sort in the second dimension, the result index is the position in the rows; and if we sort in the third dimension, the result index is the page position. This being said, it only last to produce the rows and cols for the given case:

r = repmat((1:rows)',[1 cols pages]);
c = repmat(1:cols,[rows 1 pages]);

Sorting in the first dimension is explained in the solutions given. Then, lets sort in the second dimension (row wise) of a two dimensional array

a = rand(4,5);
[rows cols pages] = size(a);
R = repmat((1:rows)',[1 cols pages]);
[sa idx] = sort(a,2);
nIdx = R + (idx-1)*rows;
isequal(sa,a(nIdx))

Now, if we use the same idea for sorting in the third dimension (page wise), we need to do

a = rand(4,5,3);
[rows cols pages] = size(a);
R = repmat((1:rows)',[1 cols pages]);
C = repmat(1:cols,[rows 1 pages]);
[sa idx] = sort(a,3);
nIdx = R + (C-1)*rows + (idx-1)*rows*cols;
isequal(sa,a(nIdx))

And the same logic can be used to extend it to N dimensions.
Thanks for your help, you en light the way. 🙂

  • 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-21T07:27:22+00:00Added an answer on May 21, 2026 at 7:27 am

    [sa, i]=sort(a) returns the ordered indices for each column. You just need to get the correct linear indices for the matrix. So, for a 2D matrix,

    A=rand(3,4);
    [rows,cols]=size(A);
    [B,index]=sort(A,1);
    correctedIndex=index+repmat(0:cols-1,rows,1)*rows;
    

    Now test it:

    A =
    
        0.9572    0.1419    0.7922    0.0357
        0.4854    0.4218    0.9595    0.8491
        0.8003    0.9157    0.6557    0.9340
    
    B =
    
        0.4854    0.1419    0.6557    0.0357
        0.8003    0.4218    0.7922    0.8491
        0.9572    0.9157    0.9595    0.9340
    
    A(correctedIndex)
    
    ans =
    
        0.4854    0.1419    0.6557    0.0357
        0.8003    0.4218    0.7922    0.8491
        0.9572    0.9157    0.9595    0.9340
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an (x,y) that is always the same for the start
Let's say i have two tables in db: Car and Part. Car owns arbitrialy
Let's assume we have the following structure: index.php config.inc.php \ lib \ lib \
Let’s say I have a number like 0x448 . In binary this is 0100
Let's say I have a 12-bit Analog to Digital Converter (4096 bins). And let's
Let's say I have a structure named vertex with a method that adds two
Let me state up front that I have an infantile understanding of Monads. I
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have window.open (without name parameter), scattered in my project and I

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.