Sort table into 2 tables

1 view (last 30 days)
Sunshine
Sunshine on 15 Apr 2024
Commented: Voss on 15 Apr 2024
I have this subset table that I've written to the attached csv. I want to sort the table such that if a user_id's score ever gets the max score all row entries for that user_id go in 1 table and rows for user_id's score that never get the max_score go in another table. Class_section is categorical and the other items are double.

Accepted Answer

Voss
Voss on 15 Apr 2024
T = readtable('instruct_04152024_update.csv');
disp(T)
user_id class_section score max_score __________ _______________ _____ _________ 2.2634e+05 {'Instructor1'} 5 5 1.3878e+06 {'Instructor4'} 0 5 1.3878e+06 {'Instructor4'} 0 5 1.3878e+06 {'Instructor4'} 0 5 1.3878e+06 {'Instructor4'} 0 5 1.5707e+06 {'Instructor3'} 0 5 1.5707e+06 {'Instructor3'} 5 5 1.7865e+06 {'Instructor5'} 5 5 1.7884e+06 {'Instructor1'} 5 5 1.7895e+06 {'Instructor2'} 0 5 1.7895e+06 {'Instructor2'} 5 5 1.7897e+06 {'Instructor2'} 5 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 5 5 1.7898e+06 {'Instructor1'} 0 5 1.7898e+06 {'Instructor1'} 5 5 1.7909e+06 {'Instructor4'} 0 5 1.7909e+06 {'Instructor4'} 5 5 1.7912e+06 {'Instructor3'} 0 5 1.7912e+06 {'Instructor3'} 0 5 1.7926e+06 {'Instructor2'} 5 5 1.7926e+06 {'Instructor6'} 5 5 1.7939e+06 {'Instructor2'} 5 5 1.7939e+06 {'Instructor6'} 5 5 1.7946e+06 {'Instructor3'} 0 5 1.7947e+06 {'Instructor3'} 5 5 1.7948e+06 {'Instructor2'} 5 5 1.796e+06 {'Instructor3'} 2 5 1.796e+06 {'Instructor3'} 2 5 1.796e+06 {'Instructor3'} 1 5 1.796e+06 {'Instructor3'} 5 5 1.7968e+06 {'Instructor4'} 5 5 1.797e+06 {'Instructor4'} 5 5 1.7979e+06 {'Instructor3'} 1 5 1.7979e+06 {'Instructor3'} 5 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7986e+06 {'Instructor6'} 5 5 1.7997e+06 {'Instructor3'} 0 5 1.7997e+06 {'Instructor3'} 0 5 1.7997e+06 {'Instructor3'} 5 5 1.8001e+06 {'Instructor3'} 5 5 1.8041e+06 {'Instructor4'} 0 5 1.8041e+06 {'Instructor4'} 5 5 1.8044e+06 {'Instructor5'} 5 5 1.8051e+06 {'Instructor3'} 5 5 1.806e+06 {'Instructor3'} 0 5 1.806e+06 {'Instructor3'} 5 5 1.81e+06 {'Instructor6'} 5 5 1.8102e+06 {'Instructor5'} 0 5 1.8102e+06 {'Instructor5'} 0 5 1.8102e+06 {'Instructor5'} 0 5 1.8102e+06 {'Instructor5'} 5 5 1.8119e+06 {'Instructor2'} 5 5 1.8144e+06 {'Instructor4'} 0 5 1.8144e+06 {'Instructor4'} 0 5 1.8144e+06 {'Instructor4'} 0 5 1.8144e+06 {'Instructor4'} 0 5 1.8255e+06 {'Instructor4'} 5 5 1.8375e+06 {'Instructor1'} 5 5
idx = ismember(T.user_id, T.user_id(T.score == T.max_score));
T1 = T(idx,:);
T2 = T(~idx,:);
disp(T1)
user_id class_section score max_score __________ _______________ _____ _________ 2.2634e+05 {'Instructor1'} 5 5 1.5707e+06 {'Instructor3'} 0 5 1.5707e+06 {'Instructor3'} 5 5 1.7865e+06 {'Instructor5'} 5 5 1.7884e+06 {'Instructor1'} 5 5 1.7895e+06 {'Instructor2'} 0 5 1.7895e+06 {'Instructor2'} 5 5 1.7897e+06 {'Instructor2'} 5 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 0 5 1.7898e+06 {'Instructor2'} 5 5 1.7898e+06 {'Instructor1'} 0 5 1.7898e+06 {'Instructor1'} 5 5 1.7909e+06 {'Instructor4'} 0 5 1.7909e+06 {'Instructor4'} 5 5 1.7926e+06 {'Instructor2'} 5 5 1.7926e+06 {'Instructor6'} 5 5 1.7939e+06 {'Instructor2'} 5 5 1.7939e+06 {'Instructor6'} 5 5 1.7947e+06 {'Instructor3'} 5 5 1.7948e+06 {'Instructor2'} 5 5 1.796e+06 {'Instructor3'} 2 5 1.796e+06 {'Instructor3'} 2 5 1.796e+06 {'Instructor3'} 1 5 1.796e+06 {'Instructor3'} 5 5 1.7968e+06 {'Instructor4'} 5 5 1.797e+06 {'Instructor4'} 5 5 1.7979e+06 {'Instructor3'} 1 5 1.7979e+06 {'Instructor3'} 5 5 1.7986e+06 {'Instructor6'} 5 5 1.7997e+06 {'Instructor3'} 0 5 1.7997e+06 {'Instructor3'} 0 5 1.7997e+06 {'Instructor3'} 5 5 1.8001e+06 {'Instructor3'} 5 5 1.8041e+06 {'Instructor4'} 0 5 1.8041e+06 {'Instructor4'} 5 5 1.8044e+06 {'Instructor5'} 5 5 1.8051e+06 {'Instructor3'} 5 5 1.806e+06 {'Instructor3'} 0 5 1.806e+06 {'Instructor3'} 5 5 1.81e+06 {'Instructor6'} 5 5 1.8102e+06 {'Instructor5'} 0 5 1.8102e+06 {'Instructor5'} 0 5 1.8102e+06 {'Instructor5'} 0 5 1.8102e+06 {'Instructor5'} 5 5 1.8119e+06 {'Instructor2'} 5 5 1.8255e+06 {'Instructor4'} 5 5 1.8375e+06 {'Instructor1'} 5 5
disp(T2)
user_id class_section score max_score __________ _______________ _____ _________ 1.3878e+06 {'Instructor4'} 0 5 1.3878e+06 {'Instructor4'} 0 5 1.3878e+06 {'Instructor4'} 0 5 1.3878e+06 {'Instructor4'} 0 5 1.7912e+06 {'Instructor3'} 0 5 1.7912e+06 {'Instructor3'} 0 5 1.7946e+06 {'Instructor3'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.7979e+06 {'Instructor6'} 0 5 1.8144e+06 {'Instructor4'} 0 5 1.8144e+06 {'Instructor4'} 0 5 1.8144e+06 {'Instructor4'} 0 5 1.8144e+06 {'Instructor4'} 0 5
  2 Comments
Sunshine
Sunshine on 15 Apr 2024
Thank you so much for this quick answer.
Voss
Voss on 15 Apr 2024
You're welcome!

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!