I need help in finding the right sequence of rows

3 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2022-3-28
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 个评论
Haseeb Hashim
Haseeb Hashim 2022-3-28
Or why do we put '~' for first output and MATLAB having these functions builtin is amazing
Walter Roberson
Walter Roberson 2022-3-29
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"

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by