S=[70 110 65 30 25 40 60]
K=[4 2 3 1 5 7 6];
[~,idx]=sort(K);
out=S(idx) Answer from Azzi Abdelmalek on mathworks.com
MathWorks
mathworks.com › matlab › data import and analysis › data preprocessing
sort - Sort array elements - MATLAB
Sort index, returned as a vector, matrix, or multidimensional array. I is the same size as A. The index vectors are oriented along the same dimension that sort operates on. For example, if A is a 2-by-3 matrix, then [B,I] = sort(A,2) sorts the elements in each row of A.
Videos
MathWorks
mathworks.com › matlabcentral › answers › 508885-how-can-i-index-elements-by-their-sorted-positions
How can I index elements by their sorted positions? - MATLAB Answers - MATLAB Central
March 4, 2020 - How can I index elements by their sorted... Learn more about index, sort, sequence MATLAB
MathWorks
mathworks.com › matlab › data import and analysis › data preprocessing
sortrows - Sort rows of matrix or table - MATLAB
For example, if A = [1 1; 2 2; 1 2; 2 2], then [Ba,Ia] = sortrows(A,'ascend') returns the sort index Ia = [1; 3; 2; 4] and [Bd,Id] = sortrows(A,'descend') returns the sort index Id = [2; 4; 3; 1]. ... Sorting by row names is not supported. For more information, see Tall Arrays. ... Cell or string input array is not supported. If A is complex with all zero imaginary parts, then MATLAB® might convert A to real(A) before calling sortrows(A).
Top answer 1 of 3
1
There are a couple of ways to achieve this:
[y1,I] = sort(x(1,:))
y2 = x(2,I)
y = [ y1 ; y2 ]
This basically sort the first row of your data, and save the sorting index in I, and then use those index to get the 'sorted' second row, and then just join them
or
y = sortrows(x')'
The ' operator transposes the matrix, which allows you to use sortrows and then use it again to reshape your output matrix.
2 of 3
1
You can use indices of sorted elements
[S,I]=sort(x(1,:));
result = [S;x(2,I)]
The first row is sorted and indices of the sorted elements is used to order the second row.
result
2 3 4 9
1 2 8 3
MathWorks
mathworks.com › matlabcentral › answers › 477515-how-to-sort-a-matrix-s-columns-by-using-indexes
How to sort a matrix's columns by using indexes - MATLAB Answers - MATLAB Central
August 25, 2019 - I'll be very greatfull to have your opinions how to sort a matrix's columns by using an array of indexes. ... https://www.mathworks.com/matlabcentral/answers/477515-how-to-sort-a-matrix-s-columns-by-using-indexes#comment_738833
MathWorks
mathworks.com › matlabcentral › answers › 1663394-obtain-the-sorted-index-vector
Obtain the sorted index vector - MATLAB Answers - MATLAB Central
March 4, 2022 - Obtain the sorted index vector. Learn more about sort, index of an element in a vector
MathWorks
mathworks.com › matlabcentral › answers › 2054579-how-can-i-use-the-returned-sort-index-from-the-sort-function-on-a-multidimensional-array
How can I use the returned sort index from the sort() function on a multidimensional array? - MATLAB Answers - MATLAB Central
November 30, 2023 - [B,I] = sort(___) also returns a collection of index vectors for any of the previous syntaxes. I is the same size as A and describes the arrangement of the elements of A into B along the sorted dimension.
MathWorks
mathworks.com › matlabcentral › answers › 2146689-how-does-indexing-work-when-sorting-a-matrix
How does indexing work when sorting a matrix? - MATLAB Answers - MATLAB Central
August 20, 2024 - For matrices, ‘sort’ orders the elements within columns. Sign in to comment. ... https://www.mathworks.com/matlabcentral/answers/2146689-how-does-indexing-work-when-sorting-a-matrix#answer_1502004
MathWorks
mathworks.com › matlabcentral › answers › 167027-how-to-index-a-sorted-array-with-the-actual-array
How to index a sorted array with the actual array? - MATLAB Answers - MATLAB Central
December 18, 2014 - I have an array, I want to sort that array and find index (location of the each element) of the sorted array elements in the actual array ... https://www.mathworks.com/matlabcentral/answers/167027-how-to-index-a-sorted-array-with-the-actual-array#comment_256410
MathWorks
mathworks.com › matlabcentral › answers › 463448-sort-an-array-while-keeping-the-right-index
Sort an array while keeping the right index - MATLAB Answers - MATLAB Central
May 22, 2019 - Look at the second output from the sort function. That can be used to generate the first output of sort from the input array, but it is not limited to being used only on that input array. Sign in to comment. Sign in to answer this question. MATLAB Language Fundamentals Matrices and Arrays Matrix Indexing
Johns Hopkins University
math.jhu.edu › ~shiffman › 370 › help › techdoc › ref › sort.html
sort (MATLAB Function Reference)
INDEX is an array of size(A), each ... value, indices are returned that preserve the original relative ordering. B = sort(A,dim) sorts the elements along the dimension of A specified by scalar dim....
MathWorks
mathworks.com › matlabcentral › answers › 368162-sort-a-matrix-according-to-the-index-of-a-vector
Sort a matrix according to the index of a vector - MATLAB Answers - MATLAB Central
November 21, 2017 - I have following labels for the rows and columns of a square matrix: labels = {'dog', 'car', 'fish'}' The corresponding matrix that expresses the semantic similarity between the terms is ...