sort a one dimensional array ascend and descend
6 ビュー (過去 30 日間)
古いコメントを表示
To sort a vector in ascending order it would seem completely obvious to type
sort([1,3,2],1,'ascend')
Who cares what the second argument is, when the first is a vector?
But matlab views [1,3,2] as a 1x3 vector, so that you have to set the second arguemnt to 2.
Since matlab can recognize a vector with
isvector
Is there any reason why the sort command doesn't simply ignore the second argument when the first is a vector? This would save many people hours of puzzlement I think.
1 件のコメント
Stephen23
2016 年 9 月 6 日
編集済み: Stephen23
2016 年 9 月 6 日
"Who cares what the second argument is, when the first is a vector?"
I do (and probably many other users too).
When I specify that I want the columns sorted, then I want the columns sorted. It does not matter one iota that the columns might have only one row, or no rows, or one million rows. Lets assume that my algorithm requires me to sort based on columns... then your proposal would mean that suddenly, in the middle of millions of loops when my data first has just one row, that MATLAB should decide that it knows better than me and will sort along the row, even though I specified the columns.
MATLAB is a tool which relies on users having some ability in keeping track of what shape and form their data is in, and hopefully also being able to read the documentation. Should MATLAB be re-designed at the level of users who cannot do this? With a line or two of code you can do all kinds of destructive, or meaningless operations: does MATLAB (or any other language) stop users from writing them? Would you really want to use a language that did not do what you told it to do?
"Is there any reason why the sort command doesn't simply ignore the second argument when the first is a vector?"
Because this would make perfectly good algorithms break, create so many bugs, and require awful, ugly workarounds. Because this would make MATLAB unusable.
採用された回答
Star Strider
2016 年 9 月 6 日
編集済み: Star Strider
2016 年 9 月 6 日
For a vector, you don’t need the second argument (row- or column-wise sorting) to be defined:
out = sort([1,3,2],'ascend')
out =
1 2 3
out = sort([1,3,2]','ascend') % Transpose To Column
out =
1
2
3
2 件のコメント
Stephen23
2016 年 9 月 6 日
編集済み: Stephen23
2016 年 9 月 6 日
" this is not an option provided by the help documentation"
Yes, it is:
B = sort(___,direction)
where the _ is a placeholder for all combinations of input arguments listed in the other syntaxes. This placeholder is the MATLAB way of writing "any number of arguments defined by other syntaxes, in this location". The explanation of that syntax also states clearly "returns sorted elements of A in the order specified by direction using any of the previous syntaxes", where the previous syntaxes include sort(A) and sort(A,dim).
This placeholder is used throughout the MATLAB documentation, eg:
M = min(A)
M = min(A,[],dim)
min(___,nanflag)
means I can call either:
min(A,nanflag)
min(A,[],dim,nanflag)
This saves lots of space in the documentation, when there are several input and output options the total number of permutations would be impractical to list and hard for a human to follow.
その他の回答 (2 件)
Thorsten
2016 年 9 月 6 日
編集済み: Thorsten
2016 年 9 月 6 日
It is consistent behaviour: You explicitly ask to sort a 1xn vector along the first dimension. I think it would be nice if Matlab issues a warning that points out that you have written a command that sorts nothing at all, or that you do not need the second numeric argument if the first is a vector.
2 件のコメント
Stephen23
2016 年 9 月 6 日
"my only complaint now is that it's not documented."
What is not documented behavior? The documented behavior is not documented ?
Mischa Kim
2016 年 9 月 6 日
Hi Leo, you can always use
sort([1,3,2],'ascend')
and even
sort([1,3,2])
As for your question: there might be scenarios where the size of the array is not known and you only want to sort along the first dimension, the rows in a 2-dim array. There might be others. But essentially, you do not want to define "standard" behavior that can have limiting or even unwanted side effects.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!