Expanding Matrix
22 ビュー (過去 30 日間)
古いコメントを表示
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
0 件のコメント
採用された回答
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?
その他の回答 (4 件)
Matt Fig
2011 年 3 月 23 日
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 件のコメント
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
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.
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
2011 年 3 月 23 日
Or, perhaps more efficient:
cell2mat(cellfun(@(x,y) times(ones(3),x),num2cell(a),'Un',0))
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!
0 件のコメント
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!