Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Blockwise Matrix Expansion

Subject: Blockwise Matrix Expansion

From: Emily Bauner

Date: 19 Jul, 2010 17:42:15

Message: 1 of 7

Hi all,

I have a matrix that I need to expand in the following way: Each element of the original matrix has to form a block in the new matrix. For instance, if I have

A = [a b; c d]

then I want to create

B = [a a b b; a a b b; c c d d; c c d d]

This would be easy to do with a 2x2 matrix; however, while A is always a square matrix its size is variable, so that I cannot just pick elements in a pre-specified manner.

I'l be really grateful for any suggestions.

Emily

Subject: Blockwise Matrix Expansion

From: Andy

Date: 19 Jul, 2010 17:59:04

Message: 2 of 7

"Emily Bauner" <emily.tting@gmail.com> wrote in message <i222pn$8e$1@fred.mathworks.com>...
> Hi all,
>
> I have a matrix that I need to expand in the following way: Each element of the original matrix has to form a block in the new matrix. For instance, if I have
>
> A = [a b; c d]
>
> then I want to create
>
> B = [a a b b; a a b b; c c d d; c c d d]
>
> This would be easy to do with a 2x2 matrix; however, while A is always a square matrix its size is variable, so that I cannot just pick elements in a pre-specified manner.
>
> I'l be really grateful for any suggestions.
>
> Emily

% Here is one solution:

A=[1 2 3;4 5 6]; % sample data
B=num2cell(A);
B=cellfun(@(x) repmat(x,2,2), B, 'UniformOutput', false);
% note: arguments to repmat determine block size
B=cell2mat(B);

Subject: Blockwise Matrix Expansion

From: Matt J

Date: 19 Jul, 2010 18:11:28

Message: 3 of 7

"Emily Bauner" <emily.tting@gmail.com> wrote in message <i222pn$8e$1@fred.mathworks.com>...
> Hi all,
>
> I have a matrix that I need to expand in the following way: Each element of the original matrix has to form a block in the new matrix. For instance, if I have
>
> A = [a b; c d]
>
> then I want to create
>
> B = [a a b b; a a b b; c c d d; c c d d]
============

For this particular example, you could do

kron(A,ones(2))

But as an FYI, these kinds of matrix expansions are often unnecessary/inefficient, which is why I created this tool as an alternative:

http://www.mathworks.com/matlabcentral/fileexchange/25969-efficient-object-oriented-kronecker-product-manipulation

Subject: Blockwise Matrix Expansion

From: Irl

Date: 19 Jul, 2010 18:25:21

Message: 4 of 7

I would use repmat plus the MATLAB (:) operator, which "unreels" a matrix column by column. Here's an example. I allow for a variable number of replicas via variable Reps:
A=reshape(1:9,3,3);Reps=3; % define variables for example case
A % display starting matrix
A2=repmat(A(:),1,Reps)';
A2=reshape(A2,size(A,1)*Reps,[]);
A2=repmat(A2,Reps,1);
A2=reshape(A2,Reps*size(A));
A2 % display result
Irl Smith

"Emily Bauner" <emily.tting@gmail.com> wrote in message <i222pn$8e$1@fred.mathworks.com>...
> Hi all,
>
> I have a matrix that I need to expand in the following way: Each element of the original matrix has to form a block in the new matrix. For instance, if I have
>
> A = [a b; c d]
>
> then I want to create
>
> B = [a a b b; a a b b; c c d d; c c d d]
>
> This would be easy to do with a 2x2 matrix; however, while A is always a square matrix its size is variable, so that I cannot just pick elements in a pre-specified manner.
>
> I'l be really grateful for any suggestions.
>
> Emily

Subject: Blockwise Matrix Expansion

From: Roger Stafford

Date: 19 Jul, 2010 18:33:06

Message: 5 of 7

"Emily Bauner" <emily.tting@gmail.com> wrote in message <i222pn$8e$1@fred.mathworks.com>...
> Hi all,
>
> I have a matrix that I need to expand in the following way: Each element of the original matrix has to form a block in the new matrix. For instance, if I have
>
> A = [a b; c d]
>
> then I want to create
>
> B = [a a b b; a a b b; c c d d; c c d d]
>
> This would be easy to do with a 2x2 matrix; however, while A is always a square matrix its size is variable, so that I cannot just pick elements in a pre-specified manner.
>
> I'l be really grateful for any suggestions.
>
> Emily
- - - - - - - -
  You could also do it this way:

 [m,n] = size(A);
 B = A(floor((p:p*(m+1)-1)/p),floor((q:q*(n+1)-1)/q));

where single elements in A are to be expanded into a p x q size block.

Roger Stafford

Subject: Blockwise Matrix Expansion

From: Roger Stafford

Date: 19 Jul, 2010 18:57:09

Message: 6 of 7

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i225p2$9bs$1@fred.mathworks.com>...
> You could also do it this way:
>
> [m,n] = size(A);
> B = A(floor((p:p*(m+1)-1)/p),floor((q:q*(n+1)-1)/q));
>
> where single elements in A are to be expanded into a p x q size block.
>
> Roger Stafford
- - - - - - -
  Or along the same lines but possible faster:

 B = A( repmat(1:size(A,1),p,1) , repmat(1:size(A,2),q,1) );

Roger Stafford

Subject: Blockwise Matrix Expansion

From: Emily Bauner

Date: 19 Jul, 2010 22:02:04

Message: 7 of 7

Thank you so much, guys, for those fast replies. It's exactly what I needed - you're awesome!

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us