matlab - Dividing a vector to form different matrices -


i have 2 long vector. vector 1 contains values of 0,1,2,3,4's, 0 represent no action, 1 represent action 1 , 2 represent second action , on. each action 720 sample point means find 720 consecutive twos 720 consecutive 4s example. vector 2 contains raw data corresponding each action. need create matrix each action ( 1, 2, 3 , 4) contains corresponding data of second vector. example matrix 1 should has data (vector 2 data) occurred @ same indices of action 1. help??

example on small amount of data: vector 1: 0 0 1 1 1 0 0 2 2 2 0 0 1 1 1 0 0 2 2 2  vector 2: 6 7 5 6 4 6 5 9 8 7 9 7 0 5 6 4 1 5 8 0 result: matrix 1: 5 6 4  0 5 6 matrix 2: 9 8 7  5 8 0 

here 1 approach. used cell array store output matrices, hard-coding names such variables isn't plan.

v1=[0 0 1 1 1 0 0 2 2 2 0 0 1 1 1 0 0 2 2 2] v2=[6 7 5 6 4 6 5 9 8 7 9 7 0 5 6 4 1 5 8 0]  %// find length of sequences of 1's/2's len=find(diff(v1(find(diff(v1)~=0,1)+1:end))~=0,1)  i=unique(v1(v1>0)); %// finds how many matrices make, 1 , 2 in case c=bsxfun(@eq,v1,i.'); %// i-th row of c contains 1's there i's in v1 %// pick out elements of v2 based on c, , store them in cell arrays matrix=arrayfun(@(m) reshape(v2(c(m,:)),len,[]).',i,'uni',0); %// note, reshape converts vector matrix  %// display results matrix{1} matrix{2} 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -