http://www.mathworks.com/matlabcentral/newsreader/view_thread/287246
MATLAB Central Newsreader  Blockwise Matrix Expansion
Feed for thread: Blockwise Matrix Expansion
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Mon, 19 Jul 2010 17:42:15 +0000
Blockwise Matrix Expansion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287246#763734
Emily Bauner
Hi all,<br>
<br>
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<br>
<br>
A = [a b; c d]<br>
<br>
then I want to create<br>
<br>
B = [a a b b; a a b b; c c d d; c c d d]<br>
<br>
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 prespecified manner. <br>
<br>
I'l be really grateful for any suggestions.<br>
<br>
Emily

Mon, 19 Jul 2010 17:59:04 +0000
Re: Blockwise Matrix Expansion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287246#763744
Andy
"Emily Bauner" <emily.tting@gmail.com> wrote in message <i222pn$8e$1@fred.mathworks.com>...<br>
> Hi all,<br>
> <br>
> 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<br>
> <br>
> A = [a b; c d]<br>
> <br>
> then I want to create<br>
> <br>
> B = [a a b b; a a b b; c c d d; c c d d]<br>
> <br>
> 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 prespecified manner. <br>
> <br>
> I'l be really grateful for any suggestions.<br>
> <br>
> Emily<br>
<br>
% Here is one solution:<br>
<br>
A=[1 2 3;4 5 6]; % sample data<br>
B=num2cell(A);<br>
B=cellfun(@(x) repmat(x,2,2), B, 'UniformOutput', false); <br>
% note: arguments to repmat determine block size<br>
B=cell2mat(B);

Mon, 19 Jul 2010 18:11:28 +0000
Re: Blockwise Matrix Expansion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287246#763749
Matt J
"Emily Bauner" <emily.tting@gmail.com> wrote in message <i222pn$8e$1@fred.mathworks.com>...<br>
> Hi all,<br>
> <br>
> 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<br>
> <br>
> A = [a b; c d]<br>
> <br>
> then I want to create<br>
> <br>
> B = [a a b b; a a b b; c c d d; c c d d]<br>
============<br>
<br>
For this particular example, you could do <br>
<br>
kron(A,ones(2))<br>
<br>
But as an FYI, these kinds of matrix expansions are often unnecessary/inefficient, which is why I created this tool as an alternative:<br>
<br>
<a href="http://www.mathworks.com/matlabcentral/fileexchange/25969efficientobjectorientedkroneckerproductmanipulation">http://www.mathworks.com/matlabcentral/fileexchange/25969efficientobjectorientedkroneckerproductmanipulation</a>

Mon, 19 Jul 2010 18:25:21 +0000
Re: Blockwise Matrix Expansion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287246#763754
Irl
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:<br>
A=reshape(1:9,3,3);Reps=3; % define variables for example case<br>
A % display starting matrix<br>
A2=repmat(A(:),1,Reps)';<br>
A2=reshape(A2,size(A,1)*Reps,[]);<br>
A2=repmat(A2,Reps,1);<br>
A2=reshape(A2,Reps*size(A));<br>
A2 % display result<br>
Irl Smith<br>
<br>
"Emily Bauner" <emily.tting@gmail.com> wrote in message <i222pn$8e$1@fred.mathworks.com>...<br>
> Hi all,<br>
> <br>
> 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<br>
> <br>
> A = [a b; c d]<br>
> <br>
> then I want to create<br>
> <br>
> B = [a a b b; a a b b; c c d d; c c d d]<br>
> <br>
> 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 prespecified manner. <br>
> <br>
> I'l be really grateful for any suggestions.<br>
> <br>
> Emily

Mon, 19 Jul 2010 18:33:06 +0000
Re: Blockwise Matrix Expansion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287246#763761
Roger Stafford
"Emily Bauner" <emily.tting@gmail.com> wrote in message <i222pn$8e$1@fred.mathworks.com>...<br>
> Hi all,<br>
> <br>
> 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<br>
> <br>
> A = [a b; c d]<br>
> <br>
> then I want to create<br>
> <br>
> B = [a a b b; a a b b; c c d d; c c d d]<br>
> <br>
> 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 prespecified manner. <br>
> <br>
> I'l be really grateful for any suggestions.<br>
> <br>
> Emily<br>
       <br>
You could also do it this way:<br>
<br>
[m,n] = size(A);<br>
B = A(floor((p:p*(m+1)1)/p),floor((q:q*(n+1)1)/q));<br>
<br>
where single elements in A are to be expanded into a p x q size block.<br>
<br>
Roger Stafford

Mon, 19 Jul 2010 18:57:09 +0000
Re: Blockwise Matrix Expansion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287246#763774
Roger Stafford
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <i225p2$9bs$1@fred.mathworks.com>...<br>
> You could also do it this way:<br>
> <br>
> [m,n] = size(A);<br>
> B = A(floor((p:p*(m+1)1)/p),floor((q:q*(n+1)1)/q));<br>
> <br>
> where single elements in A are to be expanded into a p x q size block.<br>
> <br>
> Roger Stafford<br>
      <br>
Or along the same lines but possible faster:<br>
<br>
B = A( repmat(1:size(A,1),p,1) , repmat(1:size(A,2),q,1) );<br>
<br>
Roger Stafford

Mon, 19 Jul 2010 22:02:04 +0000
Re: Blockwise Matrix Expansion
http://www.mathworks.com/matlabcentral/newsreader/view_thread/287246#763857
Emily Bauner
Thank you so much, guys, for those fast replies. It's exactly what I needed  you're awesome!