Split vector A into smaller vectors based on vector B

4 Ansichten (letzte 30 Tage)
Francisco Anaya
Francisco Anaya am 26 Mär. 2019
Bearbeitet: Francisco Anaya am 27 Mär. 2019
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 Kommentar
Francisco Anaya
Francisco Anaya am 26 Mär. 2019
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

KSSV
KSSV am 26 Mär. 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 Kommentar
Francisco Anaya
Francisco Anaya am 27 Mär. 2019
Bearbeitet: Francisco Anaya am 27 Mär. 2019
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by