Is there an efficient method to sort rows of a table, based on a particular column, in a particular order, that is not either 'ascend' or 'descend'
7 次查看(过去 30 天)
显示 更早的评论
Hello,
I believe this is probably an incredibly easy thing to do but it doesn't seem to be possible with sortrows function, based on the documentation.
I have a database, existing as a table variable, with about 20 columns of various data classes. Is there a way to sort the table using a specific order of a column? For example, if I have a table specified as the following:
opts = ["optionA";"optionB";"optionC"];
vals = [1 10 100];
table(opts,vals)
Is there a way to sort specifically by an externally defined order, perhaps by a new variable, such as:
order = ["optionB","optionA","optionC"]
I know that I could do this with forloops or something of that nature, but I find it hard to believe this capability doesnt already exist, and I think I am just skipping over something in the documentation. In addition to answering the question, can you please also provide the link to the documentation that shows me where I missed this in the first place?
Thanks!
0 个评论
回答(1 个)
Image Analyst
2019-2-3
If you have some vector that gives the order you want the vector arranged in, just use that as an index. Example:
sortedVector = vec(sortOrder);
where sortOrder is the order you want them sorted in and vec is your original vector.
另请参阅
类别
在 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!