Expanding Matrix

조회 수: 16 (최근 30일)
mo pejo
mo pejo 2011년 3월 23일
Hi all,
I have a couple of array of varying size and would like to expand it to 100 x 360 matrix.
So it has to expand in 2 dimension (row and column)
As an example;
1 2 3
4 5 6
7 8 9
to
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
7 7 7 8 8 8 9 9 9
7 7 7 8 8 8 9 9 9
7 7 7 8 8 8 9 9 9
I have tried some of the solution offered in the forum but nothing work so far.
I really hope that there are matlab expert out there that can help me with this problem. Been stuck with it for some weeks already
Regards, Mo

채택된 답변

the cyclist
the cyclist 2011년 3월 23일
Yet another:
bigA = kron(a,ones(3))
I think Matt's comment in Paulo's answer is basically an obfuscation of this, right?
  댓글 수: 2
Matt Fig
Matt Fig 2011년 3월 23일
Basically!
mo pejo
mo pejo 2011년 3월 26일
I like this answer the most. Although i need to understand the kron function more but this is the simplest.
Thanks a lot cyclist.

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

추가 답변 (4개)

Matt Fig
Matt Fig 2011년 3월 23일
I wrote the EXPAND function to do just this, in the general case, and do it efficiently.
a = [1 2 3; 4 5 6; 7 8 9];
b = expand(a,[3,3])
b =
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
1 1 1 2 2 2 3 3 3
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
4 4 4 5 5 5 6 6 6
7 7 7 8 8 8 9 9 9
7 7 7 8 8 8 9 9 9
7 7 7 8 8 8 9 9 9
  댓글 수: 2
mo pejo
mo pejo 2011년 3월 26일
For your suggestion, i have this error message.
??? Undefined function or method 'expand' for input arguments of type 'double'.
maybe i should mention that i am using matlab 2010a.
doesn't really know whether it makes any difference.
Matt Fig
Matt Fig 2011년 3월 26일
But did you download the function? That would be the first step. Then move the file to your current directory in MATLAB.

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


the cyclist
the cyclist 2011년 3월 23일
Here is one way to do it:
a = [1 2 3; 4 5 6; 7 8 9];
[nx ny] = size(a);
bigFactor = 3;
bigA = zeros(bigFactor*nx,bigFactor*ny);
for ix = 1:nx
for iy = 1:ny
bigA(1+bigFactor*(ix-1):bigFactor*ix,1+bigFactor*(iy-1):bigFactor*iy) = a(ix,iy);
end
end
This will "expand" each element to a 3x3 array, which is what you example does. If you want something more general, you might need to define a "bigFactorX" and a "bigFactorY".
There are probably more efficient ways to do this, but I hope this way is at least clear, and if your arrays are not too huge, maybe the efficiency is not your biggest concern.
  댓글 수: 1
mo pejo
mo pejo 2011년 3월 26일
This answer is usable but as you said need to include bigfactorx and y for better manipulation.

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


Paulo Silva
Paulo Silva 2011년 3월 23일
yet another way
a=[1 2 3
4 5 6
7 8 9];
c=cell(size(a));
for b=1:numel(a)
c{b}=ones(3,3)*a(b);
end
cell2mat(c)
  댓글 수: 3
Matt Fig
Matt Fig 2011년 3월 23일
Or, perhaps more efficient:
cell2mat(cellfun(@(x,y) times(ones(3),x),num2cell(a),'Un',0))
Paulo Silva
Paulo Silva 2011년 3월 23일
that's great :) I really have to learn how to use cellfun :)

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


Jos (10584)
Jos (10584) 2011년 3월 26일
No need for loops, cell2mat or cellfun. Here is the simple approach using indexing:
% data
A = [1 2 3 ; 4 5 6]
ef = [4 2] % expand factor (row and column)
% engine
[nr,nc] = size(A) ;
ri = ceil((1:(ef(1)*nr))/ef(1)) ;
ci = ceil((1:(ef(2)*nc))/ef(2)) ;
B = A(ri,ci)
% ... and as a one-liner for fun:
B1 = A(ceil((1:(ef(1)*size(A,1)))/ef(1)), ceil((1:(ef(2)*size(A,2)))/ef(2))) ;
% test
B2 = kron(A,ones(ef)) ;
isequal(B, B1, B2) % yep!

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by