MATLAB: How to get values of a matrix in MATLAB where the indices are given in a MATRIX form

matrix indexingsort

Hello! I would be extremely thankful if anyone can help me. I have a problem finding some function in MatLab which would solve my task. I use a function "sort" [ASorted, Index] = sort(A,1,'descend'); which sorts the values in matrix A along the first dimension. Is there an inverse function such that I can find out values of A using ASorted and Index? This function would use Index and depending on the dimension along which the indices are considered it would use the values in Index to identify the values in A. in this case using 1st dimension ('row dimension') we can find A.
Example: ASorted=[10 5 11 15; 8 3 9 13; 5 2 6 12; 4 1 0 7; 1 0 -4 -9]
ASorted =
10 5 11 15
8 3 9 13
5 2 6 12
4 1 0 7
1 0 -4 -9
Index = [2 2 1 4; 3 5 3 5]
Index =
2 2 1 4
3 5 3 5
In this case I would like to find only a part of A - all columns but just two rows.
I want to get
APart =
8 3 11 7
5 0 6 -9
I can't imagine there is no way to get values of A corresponding to indices in Index. If there is one function (sort), there should be another inverse, which allows without loops get values back.
I want to avoid loops and make the code fast. Thank you for any type of help!

Best Answer

n = size(ASorted);
APart = ASorted(sub2ind(n,Index,ones(size(Index,1),1)*(1:n(2))));