Split Matrix into several depending on value matlab -


i trying split nx3 matrix submatrices in matlab. matrix c of shape

c =  1     1     1  0     0     0  0     0     0  1     1     1  1     1     1  1     1     1  0     0     0  1     1     1  1     1     1 

it either has rows of zeros or rows of ones. want split matrix keep matrices of ones. here instance there's 3 'groups' of ones. want get

c1 =  1     1     1  c2 =  1     1     1  1     1     1  1     1     1  c3 =  1     1     1  1     1     1 

however real matrix n 3, don't know ones are.

edit 1:

now, need split x1 , y1 (individual row vectors same length c) x(1), x(2),.. (similarly y vector) based on how matrix c split

sample input:

x1 = (1:9)'; y1 = (2:2:18)'; 

desired output:

x(1)=[1], x(2)=[4 5 6]' , x(3)=[8 9]' y(1)=[2], y(2) =[8 10 12]' , y(3)=[16 18]' 

input:

c = [1     1     1;      0     0     0;      0     0     0;      1     1     1;      1     1     1;      1     1     1;      0     0     0;      1     1     1;      1     1     1;]      x1 = (1:9)';  y1 = (2:2:18)'; 

code:

elementallengtha = cellfun('length',regexp(sprintf('%i',all(c,2)),'1+','match')); elementalstarta = regexp(sprintf('%i',all(c,2)),'1+','start'); result = cell(length(elementallengtha),1); x = cell(length(elementallengtha),1); y = cell(length(elementallengtha),1); = 1:length(elementallengtha)     result(i) = {c(elementalstarta(i):elementalstarta(i)+elementallengtha(i)-1,:)};     x(i) = {x1(elementalstarta(i):elementalstarta(i)+elementallengtha(i)-1,:)};     y(i) = {y1(elementalstarta(i):elementalstarta(i)+elementallengtha(i)-1,:)}; end 

output:

>> cell2mat(result(1)) ans =  1     1     1  >> cell2mat(result(2)) ans =  1     1     1  1     1     1  1     1     1  >> cell2mat(result(3)) ans =  1     1     1  1     1     1  >> cell2mat(x(3))  %similarly other cells results ans =  8  9 

Comments

Popular posts from this blog

tcpdump - How to check if server received packet (acknowledged) -