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:
direct sum of matrices

Subject: direct sum of matrices

From: l.a.apiolaza@massey.ac.nz

Date: 26 Feb, 1999 01:39:30

Message: 1 of 5

Hi,

Is there a Matlab function for performing a 'direct sum' of matrices. If I
have matrices A, B, C, ..., Z dsum=[A 0 0 ... 0 0 B 0 ... 0 0 0 C ... 0 0
0 0 ... Z] i.e. a matrix with the matrices A to Z in the diagonal and null
matrices everywhere. In case there is no such function, what would be an
efficient way of implementing it?

Thanks a lot,


Luis

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Subject: direct sum of matrices

From: Peter John Acklam

Date: 26 Feb, 1999 16:44:53

Message: 2 of 5

Here is my attempt. Adding one and one and matrix would -- as far as
I know -- be less memory efficient than first figuring out what the
size of the output matrix will be.

   function d = dsum( varargin )
   %DSUM Build a matrix with the specified matrices on the main diagonal.

   nargsin = nargin;
   error( nargchk( 1, Inf, nargsin ) );

   % Calculate the size of the output matrix.
   rows = 0;
   cols = 0;
   for k = 1:nargsin
      [ r, c, d ] = size( varargin{k} );
      if d > 1
         error( 'Matrices can not have higher dimesion than 2' );
      end
      rows = rows + r;
      cols = cols + c;
   end

   d = zeros( rows, cols );
   i = 0;
   j = 0;
   for k = 1:nargsin
      [ r, c ] = size( varargin{k} );
      d( i+1:i+r , j+1:j+c ) = varargin{k};
      i = i + r;
      j = j + c;
   end

Peter

--
Peter J. Acklam - jacklam@math.uio.no - http://www.math.uio.no/~jacklam

Subject: direct sum of matrices

From: Tom Bryan

Date: 9 Mar, 1999 15:09:35

Message: 3 of 5

There is a function in the latest release of MATLAB called BLKDIAG, Block
diagonal concatenation of input arguments, that does what you want:

blkdiag(A,B,C,D,...,Z)

Best wishes,
Tom Bryan
The MathWorks, Inc.

l.a.apiolaza@massey.ac.nz wrote:

> Hi,
>
> Is there a Matlab function for performing a 'direct sum' of matrices. If I
> have matrices A, B, C, ..., Z dsum=[A 0 0 ... 0 0 B 0 ... 0 0 0 C ... 0 0
> 0 0 ... Z] i.e. a matrix with the matrices A to Z in the diagonal and null
> matrices everywhere. In case there is no such function, what would be an
> efficient way of implementing it?
>
> Thanks a lot,
>
> Luis
>
> -----------== Posted via Deja News, The Discussion Network ==----------
> http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

Subject: direct sum of matrices

From: Axel Hecht

Date: 10 Mar, 1999 10:34:59

Message: 4 of 5

Tom Bryan wrote:
>
> There is a function in the latest release of MATLAB called BLKDIAG, Block
> diagonal concatenation of input arguments, that does what you want:
>
> blkdiag(A,B,C,D,...,Z)
>
> Best wishes,
> Tom Bryan
> The MathWorks, Inc.
>
> l.a.apiolaza@massey.ac.nz wrote:
>
> > Hi,
> >
> > Is there a Matlab function for performing a 'direct sum' of matrices. If I
> > have matrices A, B, C, ..., Z dsum=[A 0 0 ... 0 0 B 0 ... 0 0 0 C ... 0 0
> > 0 0 ... Z] i.e. a matrix with the matrices A to Z in the diagonal and null
> > matrices everywhere. In case there is no such function, what would be an
> > efficient way of implementing it?
> >
> > Thanks a lot,
> >
> > Luis
> >
> > -----------== Posted via Deja News, The Discussion Network ==----------
> > http://www.dejanews.com/ Search, Read, Discuss, or Start Your Own

I read the source. At least in 5.2.1 use the other one given by Peter
Acklam.
Though you may want to modify his source to use a sparse matrix in the
end. Then you should allocate the result with the appropriate amount of
nonzeros, that can be easily calculated in the first loop.

Axel

Subject: direct sum of matrices

From: Peter John Acklam

Date: 10 Mar, 1999 17:18:54

Message: 5 of 5

Axel Hecht wrote:
>
> Tom Bryan wrote:
> >
> > There is a function in the latest release of MATLAB called BLKDIAG,
> > Block diagonal concatenation of input arguments, that does what you
> > want:
> >
> > blkdiag(A,B,C,D,...,Z)
>
> I read the source. At least in 5.2.1 use the other one given by Peter
> Acklam. Though you may want to modify his source to use a sparse
> matrix in the end. Then you should allocate the result with the
> appropriate amount of nonzeros, that can be easily calculated in the
> first loop.

Good idea! ... *mumble* ... *hack* ... done it. Default is to return
a full matrix, but can return a sparse one if you want: :-)

------ begin blockdiag.m -----

function d = blockdiag( varargin )
%BLOCKDIAG Build a block diagonal matrix of the input arguments.
%
% BLOCKDIAG( A, B, C, ... ) returns a block diagonal matrix which has
% the matrices A, B, C, ... on the main diagonal.
%
% BLOCKDIAG( A, B, C, 'sparse' ) returns the same, but as a sparse
% matrix.
%
% See also DIAG, BLKDIAG, HORZCAT, VERTCAT.

% Author: Peter J. Acklam
% Time-stamp: 1999-03-10 17:17:27
% E-mail: jacklam@math.uio.no
% WWW URL: http://www.math.uio.no/~jacklam

   if length( varargin ) < 1
      error( 'Not enough input arguments.' );
   end

   % Should we return a sparse matrix?
   want_sparse = strcmp( varargin{end}, 'sparse' );
   if want_sparse, varargin = varargin(1:end-1); end

   nargsin = length( varargin );
   if nargsin < 1
      error( 'Not enough input arguments.' );
   end

   % Calculate the size of the output matrix.
   rows = 0; % Number of rows in output matrix.
   cols = 0; % Number of columns in output matrix.
   nnz = 0; % Number of non-zero elements in output matrix.
   for k = 1:nargsin
      [ r, c, d ] = size( varargin{k} );
      if d > 1
         error( 'Matrices can not have higher dimesion than 2' );
      end
      rows = rows + r;
      cols = cols + c;
      if want_sparse
         nnz = nnz + length( find( varargin{k} ) );
         varargin{k} = sparse( varargin{k} );
      end
   end

   % Initialize output matrix.
   if want_sparse
      d = sparse( [], [], [], rows, cols, nnz );
      if nnz == 0, return, end
   else
      d = zeros( rows, cols );
   end

   % Fill the input matrices into the output matrix.
   i = 0;
   j = 0;
   for k = 1:nargsin
      [ r, c ] = size( varargin{k} );
      d( i+1:i+r , j+1:j+c ) = varargin{k};
      i = i + r;
      j = j + c;
   end

------ end blockdiag.m -----

Peter

--
Peter J. Acklam - jacklam@math.uio.no - http://www.math.uio.no/~jacklam

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