This is a static copy of a profile report

Home

mat2cell (Calls: 8, Time: 0.007 s)
Generated 16-Jul-2020 17:09:48 using performance time.
function in file /Applications/MATLAB_R2020a.app/toolbox/matlab/datatypes/mat2cell.m
Copy to new window for comparing multiple runs

Parents (calling functions)

Function NameFunction TypeCalls
array2tablefunction8
Lines where the most time was spent

Line NumberCodeCallsTotal Time% TimeTime Plot
114
c{i,j} = x(rowStart+(1:rowSize...
560.001 s14.1%
58
cellsize = cellfun('length',va...
80.001 s7.8%
87
end
160.001 s7.2%
86
vecsums(n) = sum(varargin{n}(:...
160.000 s6.4%
119
return
80.000 s5.8%
All other lines  0.004 s58.6%
Totals  0.007 s100% 
Children (called functions)
No children
Code Analyzer results
Line numberMessage
64The value assigned to variable 'vLengthInit' might be unused.
66{ A{I} } can usually be replaced by A(I) or A(I)', which can be much faster.
102When checking if a variable is a matrix consider using ISMATRIX.
Coverage results
Show coverage for parent directory
Total lines in function201
Non-code lines (comments, blank lines)111
Code lines (lines that can run)90
Code lines that did run32
Code lines that did not run58
Coverage (did run/can run)35.56 %
Function listing
time 
Calls 
 line
   1 
function c = mat2cell(x,varargin)
   2 
%MAT2CELL Break matrix up into a cell array of matrices.
   3 
%   C = MAT2CELL(X,M,N) breaks up the 2-D array X into a cell array of  
   4 
%   adjacent submatrices of X. X is an array of size [ROW COL], M is the 
   5 
%   vector of row sizes (must sum to ROW) and N is the vector of column 
   6 
%   sizes (must sum to COL). The elements of M and N determine the size of
   7 
%   each cell in C by satisfying the following formula for I = 1:LENGTH(M)
   8 
%   and J = 1:LENGTH(N),
   9 
%
  10 
%       SIZE(C{I,J}) == [M(I) N(J)]
  11 
%
  12 
%   C = MAT2CELL(X,D1,D2,D3,...,DN) breaks up the multidimensional array X
  13 
%   and returns a multidimensional cell array of adjacent submatrices of X.
  14 
%   Each of the vector arguments, D1 through DN, should sum to the
  15 
%   respective dimension sizes of X, such that for P = 1:N,
  16 
%
  17 
%       SIZE(X,P) == SUM(DP) 
  18 
%
  19 
%   The elements of D1 through DN determine the size of each cell in C by
  20 
%   satisfying the formula for IP = 1:LENGTH(DP),
  21 
%
  22 
%       SIZE(C{I1,I2,I3,...,IN}) == [D1(I1) D2(I2) D3(I3) ... DN(IN)]
  23 
%
  24 
%   C = MAT2CELL(X,R) breaks up an array X by returning a single column
  25 
%   cell array, containing the rows of X. R must sum to the number of rows
  26 
%   of X. The elements of R determine the size of each cell in C, subject
  27 
%   to the following formula for I = 1:LENGTH(R),
  28 
%
  29 
%       SIZE(C{I},1) == R(I)
  30 
%
  31 
%   C = MAT2CELL(X,...,[],...) will return an empty cell array whose empty
  32 
%   size matches the lengths of the vector arguments, D1 through DN. Note
  33 
%   that the length of an empty vector is zero.
  34 
%
  35 
%   MAT2CELL supports all array types.
  36 
%
  37 
%	Example:
  38 
%	   X = [1 2 3 4; 5 6 7 8; 9 10 11 12];
  39 
%	   C = mat2cell(X,[1 2],[1 3])
  40 
%	    
  41 
%	See also CELL2MAT, NUM2CELL
  42 

  43 
% Copyright 1984-2014 The MathWorks, Inc.
  44 

< 0.001 
      8 
  45
    narginchk(2,Inf); 
  46 
    
  47 
    % Allow use of a single input vector argument by assuming the remaining ones
  48 
    %   Remaining vectors will be the size of the input array along the
  49 
    %   respective dimension.  Necessary for backwards compatibility.
< 0.001 
      8 
  50
    if nargin == 2 
  51 
        dims = ndims(x);
  52 
        varargin{dims} = 0;
  53 
        for n=2:dims
  54 
            varargin{n} = size(x,n);
  55 
        end
< 0.001 
      8 
  56
    end 
  57 
    
< 0.001 
      8 
  58
    cellsize = cellfun('length',varargin); 
  59 
    
  60 
    % Verify that the number of input arguments meets syntax requirements.
< 0.001 
      8 
  61
    if length(varargin) ~= ndims(x) 
  62 
        % Check if the last input vectors have value 1 that would not
  63 
        %   affect the output
  64 
        vLengthInit = length(varargin);
  65 
        while (isequal(varargin{end},1)) && (length(varargin) > ndims(x))
  66 
            varargin = {varargin{1:end-1}};
  67 
        end
  68 
        
  69 
        cellsize = cellfun('length',varargin);
  70 
        
  71 
        if length(varargin) ~= ndims(x)
  72 
            error(message('MATLAB:mat2cell:ArgumentCountMismatch', ...
  73 
                length(varargin),ndims(x)));
  74 
        end
< 0.001 
      8 
  75
    end 
  76 
    
  77 
    % Verify that all dimension size arguments are vectors
< 0.001 
      8 
  78
    if ~isequal(cellsize,cellfun('prodofsize',varargin)) 
  79 
        error(message('MATLAB:mat2cell:NonVectorArgument', ...
  80 
            length(varargin)));
< 0.001 
      8 
  81
    end 
  82 
    
  83 
    % Verify that the input vectors sum to the input matrix dimensions.
< 0.001 
      8 
  84
    vecsums = zeros(size(varargin)); 
< 0.001 
      8 
  85
    for n = 1:length(vecsums) 
< 0.001 
     16 
  86
        vecsums(n) = sum(varargin{n}(:)); 
< 0.001 
     16 
  87
    end 
< 0.001 
      8 
  88
    if ~isequal(vecsums,size(x)) 
  89 
        error(message('MATLAB:mat2cell:VectorSumMismatch', length(varargin),num2str(size(x))));
< 0.001 
      8 
  90
    end 
  91 

  92 
    % If an input vector was found to be empty, return an empty cell array
  93 
    %   as described in the help. This is necessary for backward
  94 
    %   compatibility.
< 0.001 
      8 
  95
    if any(cellfun('isempty',varargin)) 
  96 
        c = cell(cellfun('length',varargin));
  97 
        return
< 0.001 
      8 
  98
    end 
  99 

 100 
    
 101 
    % If matrix is 2-D, execute 2-D code for speed efficiency
< 0.001 
      8 
 102
    if ndims(x)==2 
< 0.001 
      8 
 103
        rowSizes = varargin{1}; 
< 0.001 
      8 
 104
        colSizes = varargin{2}; 
< 0.001 
      8 
 105
        rows = length(rowSizes); 
< 0.001 
      8 
 106
        cols = length(colSizes); 
< 0.001 
      8 
 107
        c = cell(rows,cols); 
 108 
        % Construct each cell element by indexing into X with iterations of 
 109 
        %   matrix subscripts (i,j)
< 0.001 
      8 
 110
        rowStart = 0; 
< 0.001 
      8 
 111
        for i=1:rows 
< 0.001 
      8 
 112
            colStart = 0; 
< 0.001 
      8 
 113
            for j=1:cols 
  0.001 
     56 
 114
                c{i,j} = x(rowStart+(1:rowSizes(i)),colStart+(1:colSizes(j))); 
< 0.001 
     56 
 115
                colStart = colStart + colSizes(j); 
< 0.001 
     56 
 116
            end 
< 0.001 
      8 
 117
            rowStart = rowStart + rowSizes(i); 
< 0.001 
      8 
 118
        end 
< 0.001 
      8 
 119
        return 
 120 
    end
 121 
    
 122 
    % Now treat 3+ dimension arrays
 123 
    % Initialize cell array
 124 
    c = cell(cellsize);
 125 
    
 126 
    % Initialize the dimension counter, which keeps track of which cell element 
 127 
    %   we are constructing
 128 
    dimcounts = ones(size(varargin));
 129 
    dimcounts(1) = 0;
 130 
    
 131 
    % Initialize the subscript references when indexing into the matrix
 132 
    %   REFSTART is the set of matrix subscripts to be included in the first 
 133 
    %      cell element.
 134 
    %   REF is the set of matrix subscripts to be included in the loop's current
 135 
    %      cell element.
 136 
    %   TREF is a copy of REF, but replaces the 0's with []'s so that the
 137 
    %      indexing makes apprpriate empty cells instead of trying to index
 138 
    %      with 0, which would cause an error.
 139 
    %   REFSTATIC keeps track of which TREF's are [] as the dimension
 140 
    %      counter, DIMCOUNTS, increments through each of the cells. This
 141 
    %      is required since adding 0 when incrementing the references
 142 
    %      returns a value that REF needs to know, but the index should be
 143 
    %      [].
 144 
    refstart = cell(size(cellsize));
 145 
    for n=1:length(refstart)
 146 
        refstart{n} = 1:varargin{n}(1);
 147 
        if isempty(refstart{n})
 148 
            refstart{n} = 0;
 149 
        end
 150 
    end
 151 
    ref = refstart;
 152 
    ref{1} = 0;
 153 
    tref = ref;
 154 
    refstatic = zeros(1,length(cellsize));
 155 

 156 
    % Construct cell elements by looping through absolute indices and extracting
 157 
    %   the appropriate matrix subscripts
 158 
    for cind = 1:numel(c)
 159 
        % Find the next cell dimension that needs to be incremented
 160 
        inc = find(dimcounts<cellsize, 1, 'first');
 161 
        % Update the dimension counter using the incremental cell dimension
 162 
        dimcounts(1:inc) = [ones(1,inc-1) dimcounts(inc)+1];
 163 
 
 164 
        % Update the set of matrix subscripts, REF, by indexing into the input 
 165 
        %   arguments
 166 

 167 
        % If not incrementing the first cell dimension, then update REF with the
 168 
        %   appropriate earlier cell dimensions as well
 169 
        if inc~=1
 170 
            [ref{1:inc-1}] = deal(refstart{1:inc-1});
 171 
            refstatic(1:inc-1) = 0;
 172 
        end
 173 
        
 174 
        % If we are adding an empty matrix when updating this increment's
 175 
        %   reference, then add 0 instead, but index with [], and set the
 176 
        %   REFSTATIC tracker.
 177 
        if ~varargin{inc}(dimcounts(inc))
 178 
            ref{inc} = ref{inc}(end);
 179 
            refstatic(inc) = 1;
 180 
        else
 181 
            % When updating an increment's reference without adding 0,
 182 
            %   reset the REFSTATIC tracker
 183 
            ref{inc} = ref{inc}(end)+(1:varargin{inc}(dimcounts(inc)));
 184 
            refstatic(inc) = 0;
 185 
        end
 186 
                                
 187 
        % If any references are 0, change them to empty so that we are
 188 
        % indexing correctly, else let TREF{n} take on REF{n}
 189 
        for n = 1:length(refstatic)
 190 
            if ~any(ref{n}) || refstatic(n)
 191 
                tref{n} = [];
 192 
            else
 193 
                tref{n} = ref{n};
 194 
            end
 195 
        end
 196 
        
 197 
        % Finally construct the cell element by indexing into the matrix
 198 
        c{cind} = x(tref{:});
 199 

 200 
    end
 201 
end

Other subfunctions in this file are not included in this listing.