Prev: Edit mexopts.bat to compile mex file with OpenMP
Next: Powers are slow, multiplies fast; optimized badly?
From: Emily Bauner on 19 Jul 2010 13:42 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
From: Andy on 19 Jul 2010 13:59 "Emily Bauner" <emily.tting(a)gmail.com> wrote in message <i222pn$8e$1(a)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);
From: Matt J on 19 Jul 2010 14:11 "Emily Bauner" <emily.tting(a)gmail.com> wrote in message <i222pn$8e$1(a)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
From: Irl on 19 Jul 2010 14:25 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(a)gmail.com> wrote in message <i222pn$8e$1(a)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
From: Roger Stafford on 19 Jul 2010 14:33 "Emily Bauner" <emily.tting(a)gmail.com> wrote in message <i222pn$8e$1(a)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
|
Next
|
Last
Pages: 1 2 Prev: Edit mexopts.bat to compile mex file with OpenMP Next: Powers are slow, multiplies fast; optimized badly? |