How to get the order if one column has same number and other column has different values

1 view (last 30 days)
I have the below cell table in MATLAB, I am using the "num2cell(transpose(Array_games( [true, diff(Array_games) ~= 0] )));" function to get the order of the first column, and I get the order as [0 1], but I want the order of the array_games as [0 0 1], because there is gap of 3 seconds inbetween array_games 0(row 4 and 5). Is there any way I can get that by using the column 2 with array_games column?

Answers (2)

Dyuman Joshi
Dyuman Joshi on 27 Apr 2022
Array_games = [0 0 0 0 0 0 0 1 1 1]';
Column_2 = [datetime(2022,2,25,19,06,57);datetime(2022,2,25,19,06,57);datetime(2022,2,25,19,06,58);...
datetime(2022,2,25,19,06,58);datetime(2022,2,25,19,07,01);datetime(2022,2,25,19,07,02);...
datetime(2022,2,25,19,07,02);datetime(2022,2,25,19,08,33);datetime(2022,2,25,19,08,33);...
datetime(2022,2,25,19,08,33)];
y=table(Array_games, Column_2)
y = 10×2 table
Array_games Column_2 ___________ ____________________ 0 25-Feb-2022 19:06:57 0 25-Feb-2022 19:06:57 0 25-Feb-2022 19:06:58 0 25-Feb-2022 19:06:58 0 25-Feb-2022 19:07:01 0 25-Feb-2022 19:07:02 0 25-Feb-2022 19:07:02 1 25-Feb-2022 19:08:33 1 25-Feb-2022 19:08:33 1 25-Feb-2022 19:08:33
%inital 1 to address for 1st element
[1 find(diff(y.Column_2)>=duration(0,0,3))'+1] %adding one to get the required index
ans = 1×3
1 5 8
y.Array_games([1 find(diff(y.Column_2)>=duration(0,0,3))'+1])
ans = 3×1
0 0 1

Stephen23
Stephen23 on 27 Apr 2022
Edited: Stephen23 on 27 Apr 2022
Simpler and more efficient using basic logical indexing:
arg = [zeros(7,1);1;1;1]
arg = 10×1
0 0 0 0 0 0 0 1 1 1
dtm = datetime(2022,2,25,19,[6;6;6;6;7;7;7;8;8;8],[57;57;58;58;1;2;2;33;33;33])
dtm = 10×1 datetime array
25-Feb-2022 19:06:57 25-Feb-2022 19:06:57 25-Feb-2022 19:06:58 25-Feb-2022 19:06:58 25-Feb-2022 19:07:01 25-Feb-2022 19:07:02 25-Feb-2022 19:07:02 25-Feb-2022 19:08:33 25-Feb-2022 19:08:33 25-Feb-2022 19:08:33
idx = [true;diff(dtm)>=seconds(3)]
idx = 10×1 logical array
1 0 0 0 1 0 0 1 0 0
out = arg(idx)
out = 3×1
0 0 1

Tags

Community Treasure Hunt

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

Start Hunting!