Expanding Matrix

22 ビュー (過去 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 Center および File ExchangeMATLAB についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by