matrix manipulation

조회 수: 1 (최근 30일)
Max
Max 2011년 10월 20일
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 ..

답변 (2개)

David Young
David Young 2011년 10월 20일
A1 = A(1:2, 1:3);
A2 = A(1:2, 4:7);
and so on.
  댓글 수: 1
Max
Max 2011년 10월 20일
thank you

댓글을 달려면 로그인하십시오.


Andrei Bobrov
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)
  댓글 수: 1
Max
Max 2011년 10월 20일
thank you

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by