Split vector A into smaller vectors based on vector B
Mostrar comentarios más antiguos
I have two vectors of same lenght. Vector A contains angle data, and vector B contains gait phase (0=other;1=stand; 2=swing).
I want to split vector A into smallers vector for each individual gait phase cointaining only data during the stance and swing.
My data is as follows:
Vector A Vector B
1.2 1
1.3 1
1.4 1
1.5 2
1.6 2
1.7 0
1.8 0
1.9 2
1.10 2
1.11 1
1.12 1
1.13 0
1.14 1
1.15 1
Then I want to split A into...
Vector C Vector D Vector E Vector F Vector G
1.2 1.5 1.9 1.11 1.14
1.3 1.6 1.10 1.12 1.15
1.4
Thus splitting the data into small vector everytime Vector B changes but ignoring the values of A when Vector B is zero...
Thank you!
1 comentario
Francisco Anaya
el 26 de Mzo. de 2019
Respuestas (1)
KSSV
el 26 de Mzo. de 2019
data = [1.2 1
1.3 1
1.4 1
1.5 2
1.6 2
1.7 0
1.8 0
1.9 2
1.10 2
1.11 1
1.12 1
1.13 0
1.14 1
1.15 1] ;
vecA = data(:,1) ;
vecB = data(:,2) ;
[C,ia,ib] = unique(vecB) ; % groups
G = length(C) ;
iwant = cell(G,1) ;
for i = 1:G
A = (vecB==C(i))' ;
ii = zeros(size(A));
jj = A > 0;
ii(strfind([0,jj(:)'],[0 1])) = 1;
idx = cumsum(ii).*jj;
iwant{i} = accumarray( idx(jj)',vecA(jj)',[],@(x){x'});
end
1 comentario
Francisco Anaya
el 27 de Mzo. de 2019
Editada: Francisco Anaya
el 27 de Mzo. de 2019
Categorías
Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!