Split vector A into smaller vectors based on vector B
Show older comments
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 Comment
Francisco Anaya
on 26 Mar 2019
Answers (1)
KSSV
on 26 Mar 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 Comment
Francisco Anaya
on 27 Mar 2019
Edited: Francisco Anaya
on 27 Mar 2019
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!