How to generate a vector that indexes elements of another vector according to value

I have a vector of values and am looking to create a vector indexing the location of the values ordered by size. For example if:

A = [1, 0, -7, -5, 2, 12, 23]'

I want to return:

B=[7, 6, 5, 1, 2, 4, 3]'

Where B shows the location of the largest to the smallest element in A.

I understand I can use maxindc command to find the location of the maximum value but I am looking to create a ranking of all values by size, not just the maximum. Any help would be much appreciated!



1 Answer



In order to find a vector that contains the location of the largest to smallest element in a column vector you will need to use sortind procedure. GAUSS uses the sortind function to return the indices of the smallest to the largest elements in a column vector. In order to find the indices of the largest to smallest elements you can input the opposite of your vector. For example, if your vector is stored in x you would use sortind(-x). Using your example:

A = {1, 0, -7, -5, 2, 12, 23};
B = sortind(-A);

print "Sorted index of x largest to smallest (B):";

This returns:

Sorted index of x largest to smallest (B):


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy