matrix manipulation
    5 views (last 30 days)
  
       Show older comments
    
I have a matrix like this
A=[1 1 1 5 5 5 5 7 7 7; 1 1 1 5 5 5 5 7 7 7; 2 2 2 9 9 9 9 3 3 3 ; 2 2 2 9 9 9 9 3 3 3; 2 2 2 9 9 9 9 3 3 3]
i want to segregate the matrix like this
A1=[1 1 1; 1 1 1 ]
A2=[5 5 5 5; 5 5 5 5 ]
A3=[7 7 7 ; 7 7 7]
A4=[2 2 2; 2 2 2 ; 2 2 2 ]
A5=[9 9 9 9; 9 9 9 9; 9 9 9 9]
and so on.. i am not getting any idea /.. kindly help me ..
0 Comments
Answers (2)
  Andrei Bobrov
      
      
 on 20 Oct 2011
        Use a cell array Aout instead of A1, A2 ...
A=[1 1 1 5 5 5 5 7 7 7; 1 1 1 5 5 5 5 7 7 7; 2 2 2 9 9 9 9 3 3 3 ; 2 2 2 9 9 9 9 3 3 3; 2 2 2 9 9 9 9 3 3 3];
[ig1 ig2 c] = unique(A,'first');
icl = struct2cell(regionprops(reshape(c,size(A)), 'PixelList'));
[ig3,idx] = sort(cellfun(@(x)(x(1,2)-1)*size(A,1)+x(1,1),icl));
f = @(x)A(min(x(:,2)):max(x(:,2)),min(x(:,1)):max(x(:,1)));
Aout = cellfun(f,icl(idx),'un',0)
variant
data = regionprops(A,'BoundingBox');
[a b] = unique(A);
idl = cat(1,data.BoundingBox);
idl = idl(all(idl(:,3:4),2),[4 3]);
[ign,idx]= sort(b);
idx2 = reshape(reshape(idx,2,[])',[],1);
a2 = a(idx2);
idl2 = idl(idx2,:);
Aout = arrayfun(@(i1)a2(i1)*ones(idl2(i1,:)),1:numel(a2),'un',0)
small modify
data = regionprops(A,'BoundingBox');
idl = cat(1,data.BoundingBox)
lc = all(idl(:,3:4),2)
idl = ceil(idl(lc,[1 2 4 3]))
num = nonzeros((1:9)'.*lc)
[ign,idx]=sortrows([bsxfun(@minus,idl(:,1:2),[0 1])*[1;size(A,2)],idl(:,3:4)],1)
sz=ign(:,2:end)
num=num(idx)
Aout = arrayfun(@(i1)num(i1)*ones(sz(i1,:)),1:numel(num),'un',0)
small variant
a = unique(A);
m = nonzeros(arrayfun(@(x)histc(A(:,1),x),a))
n = nonzeros(arrayfun(@(x)histc(A(1,:),x),a))
Aout = reshape(mat2cell(A,m,n)',[],1)
See Also
Categories
				Find more on Whos in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!