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

 採用された回答

dpb
dpb 2022 年 7 月 4 日

0 投票

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 件のコメント

BA
BA 2022 年 7 月 4 日
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
Bruno Luong
Bruno Luong 2022 年 7 月 4 日
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
BA
BA 2022 年 7 月 4 日
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?
dpb
dpb 2022 年 7 月 4 日
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.
BA
BA 2022 年 7 月 4 日
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
dpb
dpb 2022 年 7 月 4 日
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.
Bruno Luong
Bruno Luong 2022 年 7 月 4 日
編集済み: Bruno Luong 2022 年 7 月 4 日
@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
BA
BA 2022 年 7 月 4 日
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.
dpb
dpb 2022 年 7 月 4 日
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...
BA
BA 2022 年 7 月 4 日
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
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.
BA
BA 2022 年 7 月 4 日
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 ExchangeShifting and Sorting Matrices についてさらに検索

製品

リリース

R2022a

タグ

質問済み:

BA
2022 年 7 月 4 日

コメント済み:

BA
2022 年 7 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by