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!
Thanks,
ZC
1 Answer
0
accepted
ZC,
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):";
B;
This returns:
Sorted index of x largest to smallest (B):
7.0000
6.0000
5.0000
1.0000
2.0000
4.0000
3.0000
Your Answer
1 Answer
ZC,
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):";
B;
This returns:
Sorted index of x largest to smallest (B):
7.0000
6.0000
5.0000
1.0000
2.0000
4.0000
3.0000