How to split the data inside the matrix correctly?

1 次查看(过去 30 天)
Hi! I have a matrix with two columns, the first one is time and the second one is trigger which has only 0 and 1. I want to split the data intro the groups that the first few zeros will be one group, then the first 1s will be next group, then the zeros again will be another group and so on. For example the matrix A is:
0 0
2.53164556962025 0
5.06329113924051 0
7.59493670886076 0
10.1265822784810 1
12.6582278481013 1
15.1898734177215 1
17.7215189873418 0
20.2531645569620 0
22.7848101265823 0
25.3164556962025 1
27.8481012658228 1
30.3797468354430 1
32.9113924050633 1
35.4430379746836 1
37.9746835443038 0
40.5063291139241 0
43.0379746835443 1
45.5696202531646 1
48.1012658227848 1
50.6329113924051 1
So the in the end I want to get 6 different matrices: the first one will be with first 4 zeros, the next one with 3 ones, then with 3 zeros, then with 5 ones and so on.
Thanks for your help!

采纳的回答

Tommy
Tommy 2020-5-28
Here's another uglier way.
C = mat2cell(matrix, diff([0; find(diff(matrix(:,2))); size(matrix,1)]), 2);

更多回答(1 个)

KSSV
KSSV 2020-5-28
Try this loop....I quicky typed it..check thorughly.
data = [0 0
2.53164556962025 0
5.06329113924051 0
7.59493670886076 0
10.1265822784810 1
12.6582278481013 1
15.1898734177215 1
17.7215189873418 0
20.2531645569620 0
22.7848101265823 0
25.3164556962025 1
27.8481012658228 1
30.3797468354430 1
32.9113924050633 1
35.4430379746836 1
37.9746835443038 0
40.5063291139241 0
43.0379746835443 1
45.5696202531646 1
48.1012658227848 1
50.6329113924051 1];
iwant = cell([],1) ; % initialize the final result into cells
N = size(data,1) ;
count = 1 ;
pos = 0 ;
val = data(:,1) ;
for i = 1:N
val1 = data(i,2) ;
if val ~= val1
count = count+1 ;
val = val1;
pos = 0 ;
end
pos = pos+1 ;
iwant{count}(pos,1:2) = data(i,:) ;
end
iwant

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by