2D Array Manipulation / Script

I am looking to write a simple MATLAB script that will take any 2D array and double its size by duplicating every entry in the horizontal, vertical, and diagonal direction. The catch is I can not use for-loops. I have been thinking about using repmat(), but am unsure how this could translate to my problem.
For example, if I have a matrix A = [1 4 7 ; 8 9 3], then the result should be:
A=[
1 1 4 4 7 7
1 1 4 4 7 7
8 8 9 9 3 3
8 8 9 9 3 3
]
I hope I am articulating my thoughts well. Any help or tips would be greatly appreciated.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 6일
편집: Azzi Abdelmalek 2012년 9월 6일

0 개 추천

%method1
res=cell2mat(arrayfun(@(x) repmat(x,2,2),A,'uni',false))
%or method 2
[n,m]=size(A)
B=zeros(n,2*m);
B(:,1:2:2*m)=A;B(:,2:2:2*m)=A
C=zeros(2*n,2*m);
C(1:2:2*n,:)=B;C(2:2:2*n,:)=B

댓글 수: 2

Jason Kossis
Jason Kossis 2012년 9월 6일
Thanks for the help!
Method 3:
kron(A,ones(2))

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

추가 답변 (1개)

Matt Fig
Matt Fig 2012년 9월 6일

0 개 추천

You can also use expand, which is written to be more general and memory efficient.
A = [1 4 7 ; 8 9 3],
expand(A,[2,2])
ans =
1 1 4 4 7 7
1 1 4 4 7 7
8 8 9 9 3 3
8 8 9 9 3 3

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by