I need help in finding the right sequence of rows

2 views (last 30 days)
I have the column array of
A = [2 3 2 5 6 1]'; % This is a column as I have transposed the row
Now I need the code or any hint which gives me the index of rows having the increasing order i-e from the array the row with lowest number is 6 and then 1 as "1" comes before "3" as we have a tie between rows 1 and 3 but as 1 comes before 3 so we take the row 1 as next so, like this we have the following order of row desired
6,1,3,2,4,5
As 6th row contains the smallest element and next comes 1st row and then comes the 3 rd row and then comes 2nd row followed by 4th row which contains 5 and then lastly the 5th row which contains the largest element
I have tried a lot but couldnt come up with the logic

Accepted Answer

Walter Roberson
Walter Roberson on 28 Mar 2022
A = [2 3 2 5 6 1]'
A = 6×1
2 3 2 5 6 1
[~, order] = sort(A)
order = 6×1
6 1 3 2 4 5
  4 Comments
Haseeb Hashim
Haseeb Hashim on 28 Mar 2022
Or why do we put '~' for first output and MATLAB having these functions builtin is amazing
Walter Roberson
Walter Roberson on 29 Mar 2022
Why is there a not equal sign in output
?? I am not sure what you are referring to? The output does have an equal sign:
order = 6×1
and the assignment statement has an equal sign
[~, order] = sort(A)
^
Or why do we put '~' for first output
When you list ~ as the destination for an assignment, that means that the output should be discarded. Equivalent code without using that would be
[temp23318546895423236264832667075098, order] = sort(A);
clear temp23318546895423236264832667075098
display(order)
Can you please explain how this code works
MATLAB uses QuickSort internally. It copies the input vector, and it also creates a vector which is the integers 1 to the number of elements. The process of sorting the vector involves interchanging elements to move them into a more-sorted order, and each time it exchange elements of the main numeric vector, it also exchanges elements of the index vector. At the end, you get out the sorted vector of values, and you also get out a vector that indicates where each element of the sorted vector originally "came from"

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!