matrix manipulation
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (2 件)
Andrei Bobrov
2011 年 10 月 20 日
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)
参考
カテゴリ
Help Center および File Exchange で Matrices and Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!