Split vector A into smaller vectors based on vector B

1 次查看(过去 30 天)
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 个评论
Francisco Anaya
Francisco Anaya 2019-3-26
Before I was using the following, but I didnt have the third condition in B when b is equal to zero...
A = [1.2;1.3;1.4;1.5;1.6;1.7;1.8;1.9;1.1;1.11;1.12;1.13;1.14];
B = [1;1;1;2;2;2;2;2;2;1;1;1;1];
X = cumsum([true;0~=diff(B)])
C = accumarray(X,A,[],@(v){v});
C{:}
ans =
1.2000
1.3000
1.4000
ans =
1.5000
1.6000
1.7000
1.8000
1.9000
1.1000
ans =
1.1100
1.1200
1.1300
1.1400

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2019-3-26
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 个评论
Francisco Anaya
Francisco Anaya 2019-3-27
编辑:Francisco Anaya 2019-3-27
Dear KSSV thank you for your answer. But I have one problem. I have implemented your code as follows:
[C,ia,ib]=unique(r_gait_phase_new); %groups
G=length(C);
iwant=cell(G,1);
for i=1:G
A=(r_gait_phase_new==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)',angle_data_new(jj)',[],@(x){x'});
end
The code works well. However, angle_data_new contains 6 vector data (for 6 different angles, foot, knee and hip). The code you shared only split the data of the first column. How can I obtain the splitted vectors for all 6 vectors contained in angle_data_new??
Thank you so much! You are a lifesaver!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by