I need help in finding the right sequence of rows
1 次查看(过去 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
0 个评论
采纳的回答
Walter Roberson
2022-3-28
A = [2 3 2 5 6 1]'
[~, order] = sort(A)
4 个评论
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 Center 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!