Expanding Matrix
조회 수: 16 (최근 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!