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 Ansichten (letzte 30 Tage)
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!

Antworten (1)

Image Analyst
Image Analyst am 3 Feb. 2019
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.
  1 Kommentar
therapy_inc
therapy_inc am 3 Feb. 2019
Ok, so yes, that would work, but I don't actually have a index vector like sortOrder. I definitly could make that but that would probably require some kind of loop across every row, which seems a bit bulky.
P.S. I've read your comments before on image stuff alot - thanks for those!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Shifting and Sorting Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by