Home > FMAToolbox > General > Array2PagedMatrix.m

Array2PagedMatrix

PURPOSE ^

Array2PagedMatrix - Transform an N-dimensional array into a paged matrix.

SYNOPSIS ^

function M = Array2PagedMatrix(A)

DESCRIPTION ^

Array2PagedMatrix - Transform an N-dimensional array into a paged matrix.

  Vertically concatenate all pages in the array, and add N-1
  columns listing the subscripts for all dimensions except the
  second dimension (which is implicit, since it becomes the
  second dimension of the output matrix).

  USAGE

    M = Array2Matrix(A)

    A              N-dimensional array

  EXAMPLE

    Consider the following example. Let A be the array:

    A(:,:,1,1) =

       1     2     3     4     5
       6     7     8     9     0


    A(:,:,2,1) =

       0     9     8     7     6
       5     4     3     2     1


    A(:,:,1,2) =

       4     4     4     4     4
       5     5     5     5     5


    A(:,:,2,2) =

       6     6     6     6     6
       7     7     7     7     7


    A(:,:,1,3) =

       8     8     8     8     8
       9     9     9     9     9


    A(:,:,2,3) =

       0     0     0     0     0
       6     5     6     5     6

    Array2PagedMatrix concatenates each of the above pages vertically, and adds three
    columns listing the first, third and fourth dimensions, yielding:

    B =

       1     2     3     4     5     1     1     1
       6     7     8     9     0     2     1     1
       0     9     8     7     6     1     2     1
       5     4     3     2     1     2     2     1
       4     4     4     4     4     1     1     2
       5     5     5     5     5     2     1     2
       6     6     6     6     6     1     2     2
       7     7     7     7     7     2     2     2
       8     8     8     8     8     1     1     3
       9     9     9     9     9     2     1     3
       0     0     0     0     0     1     2     3
       6     5     6     5     6     2     2     3

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function M = Array2PagedMatrix(A)
0002 
0003 %Array2PagedMatrix - Transform an N-dimensional array into a paged matrix.
0004 %
0005 %  Vertically concatenate all pages in the array, and add N-1
0006 %  columns listing the subscripts for all dimensions except the
0007 %  second dimension (which is implicit, since it becomes the
0008 %  second dimension of the output matrix).
0009 %
0010 %  USAGE
0011 %
0012 %    M = Array2Matrix(A)
0013 %
0014 %    A              N-dimensional array
0015 %
0016 %  EXAMPLE
0017 %
0018 %    Consider the following example. Let A be the array:
0019 %
0020 %    A(:,:,1,1) =
0021 %
0022 %       1     2     3     4     5
0023 %       6     7     8     9     0
0024 %
0025 %
0026 %    A(:,:,2,1) =
0027 %
0028 %       0     9     8     7     6
0029 %       5     4     3     2     1
0030 %
0031 %
0032 %    A(:,:,1,2) =
0033 %
0034 %       4     4     4     4     4
0035 %       5     5     5     5     5
0036 %
0037 %
0038 %    A(:,:,2,2) =
0039 %
0040 %       6     6     6     6     6
0041 %       7     7     7     7     7
0042 %
0043 %
0044 %    A(:,:,1,3) =
0045 %
0046 %       8     8     8     8     8
0047 %       9     9     9     9     9
0048 %
0049 %
0050 %    A(:,:,2,3) =
0051 %
0052 %       0     0     0     0     0
0053 %       6     5     6     5     6
0054 %
0055 %    Array2PagedMatrix concatenates each of the above pages vertically, and adds three
0056 %    columns listing the first, third and fourth dimensions, yielding:
0057 %
0058 %    B =
0059 %
0060 %       1     2     3     4     5     1     1     1
0061 %       6     7     8     9     0     2     1     1
0062 %       0     9     8     7     6     1     2     1
0063 %       5     4     3     2     1     2     2     1
0064 %       4     4     4     4     4     1     1     2
0065 %       5     5     5     5     5     2     1     2
0066 %       6     6     6     6     6     1     2     2
0067 %       7     7     7     7     7     2     2     2
0068 %       8     8     8     8     8     1     1     3
0069 %       9     9     9     9     9     2     1     3
0070 %       0     0     0     0     0     1     2     3
0071 %       6     5     6     5     6     2     2     3
0072 %
0073 
0074 
0075 % Copyright (C) 2008-2011 by Michaƫl Zugaro
0076 %
0077 % This program is free software; you can redistribute it and/or modify
0078 % it under the terms of the GNU General Public License as published by
0079 % the Free Software Foundation; either version 3 of the License, or
0080 % (at your option) any later version.
0081 
0082 %  A(:,:,1,1) = [1 2 3 4 5;6 7 8 9 0];
0083 %  A(:,:,2,1) = [0 9 8 7 6;5 4 3 2 1];
0084 %  A(:,:,1,2) = [4 4 4 4 4;5 5 5 5 5];
0085 %  A(:,:,2,2) = [6 6 6 6 6;7 7 7 7 7];
0086 %  A(:,:,1,3) = [8 8 8 8 8;9 9 9 9 9];
0087 %  A(:,:,2,3) = [0 0 0 0 0;6 5 6 5 6];
0088 
0089 if nargin < 1,
0090     error('Incorrect number of parameters (type ''help <a href="matlab:help Array">Array</a>2PagedMatrix'' for details).');
0091 end
0092 
0093 if length(size(A)) <= 2
0094     M = A;
0095     return
0096 end
0097 
0098 % The array A contains k pages, each of which is a m x n matrix. Let d be the number of
0099 % dimensions. The output matrix should have size k*m x (n+d-1)
0100 m = size(A,1);
0101 n = size(A,2);
0102 d = length(size(A));
0103 k = prod(size(A))/(m*n);
0104 M = zeros((k*m),(n+d-1));
0105 
0106 % This string will be evaluated later to determine for each page the subscripts for the d extra dimensions
0107 % (we need to use this trick because we do not know how many extra dimensions there are)
0108 getSubscripts = '[';
0109 for i = 1:d,
0110     getSubscripts = [getSubscripts 'v' int2str(i) ','];
0111 end
0112 getSubscripts = [getSubscripts(1:end-1) ']=ind2sub(size(A),i*s);'];
0113 
0114 % This string will be evaluated later to set the last d columns (corresponding to the subscripts for the d extra dimensions)
0115 % (we need to use this trick because we do not know how many extra dimensions there are)
0116 setSubscripts = 'M((i-1)*m+1:i*m,n+2:end)=repmat([';
0117 for i = 3:d,
0118     setSubscripts = [setSubscripts 'v' int2str(i) ','];
0119 end
0120 setSubscripts = [setSubscripts(1:end-1) '],m,1);'];
0121 
0122 s = m*n;
0123 for i = 1:k,
0124     % Get i-th page
0125     M((i-1)*m+1:i*m,1:n) = reshape(A((i-1)*s+1:i*s),m,n);
0126     % Set the last d columns (corresponding to the subscripts for the extra dimensions)
0127     eval(getSubscripts);
0128     eval(setSubscripts);
0129 end
0130 
0131 M(:,n+1) = repmat((1:m)',k,1);

Generated on Fri 16-Mar-2018 13:00:20 by m2html © 2005