Code covered by the BSD License  

Highlights from
Matrix Library for Simulink

image thumbnail
from Matrix Library for Simulink by Jay St. Pierre
A set of blocks for manipulating 3x3 and 4x4 matrices.

Amat=rm2mat(A,cols)
function Amat=rm2mat(A,cols)
% RM2MAT(A,N) converts matrices from row major form to regular matrix form.
%     The input A can be a single vector which represents a matrix in row
%     major form, or it can be a matrix whose rows represent matrices in row
%     major form.  If A is a matrix, the output, AOUT, is a 3-dimensional
%     array whose third dimension corresponds to the rows of A.  For
%     example, A(2,:) corresponds to AOUT(:,:,2).
%
%     Without a second argument, matrices are assumed to be square,
%     therefore, the number of columns of A must be a square of an
%     integer. If the argument N is supplied, RM2MAT() converts each the
%     rows of A into a matrix with N columns, thus the number of columns in
%     A must be evenly divisible by N.

% $Source: /home/stpierre/cvsroot/matlab/simulink/matrix/rm2mat.m,v $
% $Revision: 1.2 $
% $Date: 2009-07-19 22:03:05 $
 
% Copyright (c) 2000-2009, Jay A. St. Pierre.  All rights reserved.


if nargin<1
  error('rm2mat() requires at least one input argument');
end

if ( length(size(A)) ~= 2)
  error('Input must be a vector or a matrix, not a higher order array')
end
  
if nargin==1
  cols=sqrt(size(A,2));
  rows=size(A,2)/cols;
  if ( cols ~= round(cols) )
    error(['Number of columns of input must be a square of an integer,', ...
          10, 'or you must specify the number of columns you want the' ...
          10, 'output matrices to have.']);
  end
else % nargin==2
  rows=size(A,2)/cols;
  if ( rows ~= round(rows) )
    error(['Number of columns of input is not evenly divisble by ' ...
          num2str(cols)])
  elseif ( rows == 1 )
    warning('rm2mat:singleRow', ...
            'You may want to use PERMUTE instead of rm2mat');
  end
end

num  = size(A,1);              % The total number of matrices
Amat = zeros(rows, cols, num); % Initialize to avoid growth inside loop
for k=1:num
  for j=1:rows
    first_col=((j-1)*cols)+1;
    Amat(j,1:cols,k)=A(k,first_col:first_col+cols-1);
  end
end

Contact us at files@mathworks.com