From: Sumeet on 22 Mar 2010 09:52 "Marvin Bugeja" <mervremovethis(a)ieee.org> wrote in message <grpqbl$r9u$1(a)fred.mathworks.com>... > Hi, > I came across this very simply task explained below: > > Say a 3D matrix G is defined as follows: > G(:,:,1) = [1,2;3,4]; > G(:,:,2) = [5,6;7,8]; > How can one achieve a 2D matrix that looks like: > G2D = [1,2;3,4;5,6;7,8]; > > In other words, I would like to get the 2x2 blocks from the 3D matrix and concatenate them vertically under each other. > (N.B: this operation would later be used in a block multiplication operation, but this is the main problem). I solved it by converting the matrix to a cell and then use vertcat(), but is there any other way! (maybe faster!). No for loops please :-) > - Thanks Does somebody have a general method that works for any a x b x c (3 dimensional) matrix? Implementing the above kind of methods would require for loops.
From: Sumeet on 22 Mar 2010 10:07 "Sumeet " <sumeetsk(a)gmail.com> wrote in message <ho7sm5$pn9$1(a)fred.mathworks.com>... > "Marvin Bugeja" <mervremovethis(a)ieee.org> wrote in message <grpqbl$r9u$1(a)fred.mathworks.com>... > > Hi, > > I came across this very simply task explained below: > > > > Say a 3D matrix G is defined as follows: > > G(:,:,1) = [1,2;3,4]; > > G(:,:,2) = [5,6;7,8]; > > How can one achieve a 2D matrix that looks like: > > G2D = [1,2;3,4;5,6;7,8]; > > > > In other words, I would like to get the 2x2 blocks from the 3D matrix and concatenate them vertically under each other. > > (N.B: this operation would later be used in a block multiplication operation, but this is the main problem). I solved it by converting the matrix to a cell and then use vertcat(), but is there any other way! (maybe faster!). No for loops please :-) > > - Thanks > > Does somebody have a general method that works for any a x b x c (3 dimensional) matrix? Implementing the above kind of methods would require for loops. Sorry for answering my own question, but I figured it out. Posting the general method in case anybody else needs it: function [twodmatrix] = planes2matrix (threedmatrix) n_columns = size(threedmatrix,2); n_rows = size(threedmatrix,1) * size(threedmatrix,3); twodmatrix_1 = permute (threedmatrix,[2,1,3]); twodmatrix_2 = reshape (twodmatrix_1,n_columns,n_rows); twodmatrix = twodmatrix_2'; endfunction
|
Pages: 1 Prev: Krylov subspaces for householder matrix??? Next: HSPICE Toolbox for MATLAB |