Expanding Matrix

16 views (last 30 days)
mo pejo
mo pejo on 23 Mar 2011
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

Accepted Answer

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

Sign in to comment.

More Answers (4)

Matt Fig
Matt Fig on 23 Mar 2011
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 Comments
mo pejo
mo pejo on 26 Mar 2011
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 on 26 Mar 2011
But did you download the function? That would be the first step. Then move the file to your current directory in MATLAB.

Sign in to comment.


the cyclist
the cyclist on 23 Mar 2011
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 Comment
mo pejo
mo pejo on 26 Mar 2011
This answer is usable but as you said need to include bigfactorx and y for better manipulation.

Sign in to comment.


Paulo Silva
Paulo Silva on 23 Mar 2011
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 Comments
Matt Fig
Matt Fig on 23 Mar 2011
Or, perhaps more efficient:
cell2mat(cellfun(@(x,y) times(ones(3),x),num2cell(a),'Un',0))
Paulo Silva
Paulo Silva on 23 Mar 2011
that's great :) I really have to learn how to use cellfun :)

Sign in to comment.


Jos (10584)
Jos (10584) on 26 Mar 2011
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!

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!