Sort twice within the same dataset
显示 更早的评论
I'm working with a really large data set and I was hoping to write some code to sort within the same data set twice. This might sound confusing but specifically what I'm trying to do is sort one column, and while keeping the sort for that row, I want to sort another column.
If anyone is aware of a way to do this, please let me know.
Thanks
采纳的回答
Well, it's certainly not totally clear what you mean -- but if you mean sort by multiple columns, see sortrows, the optional second argument, column
12 个评论
Basically, this is how I want to sort the data.

Column 1 is the user IDs, and then column 2 would be the dates I want in chronological order. But the catch is, each date is associated with survey data so I need those to sort while staying with the data. Let me know if this still makes no sense
So you can call sortrows as dpb suggests,
or you can also do 2 steps
sort by date, then
sort by UserID on the result of sort by date
But won't I jumble up the rest of my data then?
I'll give a better example. So say for example:

Would a command like this work?
Why not try it and see? <VBG>
But, yes, that's precisely what sortrows does; it orders by the column(s) selected keeping records together. It doesn't move individual columns, it rearranges records/rows by the sequence requested.
To do the same with sort by steps, note you would have to save the sort order of the column and then use it to rearrange the whole row in that order; by itself sort will rearrange the column itself, NOT the entire row/record.
Doesn't work, I think it might be the data set that I'm working with. I'll look into it but thanks for the help. I appreciate it
What doesn't work? Show us precisely what you did and what you expected that isn't as returned by sortrows to match the request.
BTW, we can't do anything with images; attach text for example file or a .mat file.
@Syed Sarwar "Doesn't work"
UserID=50000+randi(3,10,1);
Date=randi(3,10,1);
Data=table(UserID,Date)
Data = 10×2 table
UserID Date
______ ____
50001 1
50001 1
50002 3
50003 1
50003 2
50001 1
50001 1
50001 1
50001 1
50002 3
%use sortrows
Datas = sortrows(Data,[1 2])
Datas = 10×2 table
UserID Date
______ ____
50001 1
50001 1
50001 1
50001 1
50001 1
50001 1
50002 3
50002 3
50003 1
50003 2
% 2-step sort
[~,is2]=sort(Data.Date);
[~,is1]=sort(Data(is2,:).UserID);
Datas=Data(is2(is1),:)
Datas = 10×2 table
UserID Date
______ ____
50001 1
50001 1
50001 1
50001 1
50001 1
50001 1
50002 3
50002 3
50003 1
50003 2
The problem that's occuring is that the data for my file is in the DD/MM/YYYY format, and so matlab is interpreting it as MM/DD/YYYY when importing.
So the data in my file is
4/8/2021 which is supposed to be August 4, 2021
But since MATLAB interprets dates as MM-DD-YYYY
The date ends up reading as April 8, 2021 so that's why it isn't sorting correctly but the command itself works. What I'm thinking of doing is sorting the table I have by UserID + response ID, so [1 3] and then creating my own dates based on the user's start date and end date.
Convert the date strings to datetime values and they'll sort chronologically. You can set the input format if it isn't being interpreted correctly but generally unless there are no days outside 1-12 in the portion of the file that is parsed to autodetect formatting, it is possible it will get month and day fields confused.
Again, attach a short sample file and show us the code you're using to read it with and somebody can help fix your issues...
I have attached a snippet of the data to this message.
There are dates missing from the dataset too so I'm going to have to ask my coordinator what to do because I can't proceed without the dates. If you can make something of it though, let me know
Bruno Luong
2022-7-4
编辑:Bruno Luong
2022-7-4
"If you can make something of it though, let me know"
No it's your data, no one can do "'something of it"
So you discover that your importation reverses date and month and has missing date in the data base. It has nothing with the original question you asked about sorting.
It seems you can accept dpb answer and move on, and ask another question if you are stuck by other issues.
Well he asked for the data. I was ready to move on after the third message because I realized there were issues with the data itself.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息
标签
另请参阅
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
