# Help extracting rows based on unique column 1 data

4 views (last 30 days)
Brantosaurus on 19 Nov 2022
Answered: Campion Loong on 30 Nov 2022
I have a large MxN matrix of data from which i would like to extract specific rows of data.
The first column of the matrix contains a chronological timebase in seconds, in ascending order.
t1 a1 b1 .. n1
t2 a2 b2 .. n2
:
talpha ..
:
tbeta ..
:
tgamma ..
:
tm am bm .. nm
I know the unique times of 3 events (talpha, tbeta, tgamma) that occur in column 1.
How do i extract the rows in chronological order based on the knowledge of their unique times?
Tried categorical with valueset and ordinal but no luck!
Image Analyst on 19 Nov 2022
How large is large? How many GB or rows? If it really is large, like hundreds of millions of rows or several GB of data then you may need to use memmapfile. If you just have a few million rows or less, that's not large.
Brantosaurus on 19 Nov 2022
Moved: Voss on 19 Nov 2022
Not THAT large!!!
Maybe 10,000 x 20 at most

Star Strider on 19 Nov 2022
However, if you want to find the unique occurrences of each of the ‘talpha’ , ‘tbeta’ and ‘tgamma’ so that they return the indices of the first incidence of each value, use the unique function with the first two outputs (the unique values, and the indices of the first instance of each value) with the 'stable' argument (to prvent sorting the result).
Brantosaurus on 22 Nov 2022
Apologies for the vagueness. Had a play around and came up with something following your excelent advice. Thanks for mentioning the unique function and indices. Not heard of those before. Best regards.
Star Strider on 22 Nov 2022
As always, my pleasure!

Campion Loong on 30 Nov 2022
I would first put my data in a timetable. It is specifically built for time based data like yours.
% Make some pretend data for your a#/b#/c# numbers
A = rand(10000,1); B = rand(10000,1); C = rand(10000,1);
% Use timetable. Here I assume regular timesteps. If not, use the
tt = timetable(A, B, C, 'TimeStep', seconds(1))
tt = 10000×3 timetable
Time A B C ________ ________ _______ ________ 00:00:00 0.046518 0.25765 0.11216 00:00:01 0.021558 0.19586 0.078626 00:00:02 0.325 0.81709 0.9364 00:00:03 0.11313 0.10249 0.36468 00:00:04 0.65928 0.43818 0.4145 00:00:05 0.59172 0.30821 0.52174 00:00:06 0.39627 0.23423 0.42336 00:00:07 0.42042 0.62615 0.8902 00:00:08 0.62093 0.60716 0.098711 00:00:09 0.030026 0.18906 0.49313 00:00:10 0.5556 0.51185 0.6461 00:00:11 0.65488 0.38215 0.59968 00:00:12 0.48795 0.79178 0.69614 00:00:13 0.040898 0.99794 0.59517 00:00:14 0.17782 0.81708 0.12582 00:00:15 0.64031 0.78373 0.81385
If you know the exact time of your unique events
t_alpha = seconds(10);
t_beta = minutes(150);
t_gamma = hours(2);
% Index directly with time. Get all the rows in one go like below, or
tt([t_alpha; t_beta; t_gamma], :)
ans = 3×3 timetable
Time A B C ________ _______ _______ _______ 00:00:10 0.5556 0.51185 0.6461 02:30:00 0.44544 0.32722 0.23712 02:00:00 0.48217 0.21062 0.37704
Now, if you are not sure the 'exact' time (e.g. your data has some noise)
tt.Time = tt.Time + milliseconds(randn(10000,1)); % mix in some made-up noise
% Exact time index will turn up nothing ...
tt([t_alpha; t_beta; t_gamma], :)
ans = 0×3 empty timetable
% Use withtol to index with time tolerance:
wt = withtol([t_alpha; t_beta; t_gamma],seconds(0.5))
wt =
timetable withtol subscript: Select timetable rows matching the following times: 10 sec 9000 sec 7200 sec with tolerance of +/- 0.5 sec See Select Times in Timetable.
tt(wt, :)
ans = 3×3 timetable
Time A B C ________ _______ _______ _______ 00:00:10 0.5556 0.51185 0.6461 02:30:00 0.44544 0.32722 0.23712 02:00:00 0.48217 0.21062 0.37704

### Categories

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

### Community Treasure Hunt

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

Start Hunting!