Home > marsbar > @mardo_5 > private > pr_spm_cat.m

pr_spm_cat

PURPOSE ^

converts a cell array into a matrix

SYNOPSIS ^

function [x] = pr_spm_cat(x,d)

DESCRIPTION ^

 converts a cell array into a matrix
 FORMAT [x] = pr_spm_cat(x,d);
 x - cell array
 d - dimension over which to concatenate [default - both]
__________________________________________________________________________
 Empty array elements are replaced by sparse zero partitions
 and single 0 entries are expanded to conform to the non-empty
 non zero elements.

 e.g.:
 > x       = pr_spm_cat({eye(2) []; 0 [1 1; 1 1]})
 > full(x) =

     1     0     0     0
     0     1     0     0
     0     0     1     1
     0     0     1     1
__________________________________________________________________________
 Copyright (C) 2005 Wellcome Department of Imaging Neuroscience

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [x] = pr_spm_cat(x,d)
0002 % converts a cell array into a matrix
0003 % FORMAT [x] = pr_spm_cat(x,d);
0004 % x - cell array
0005 % d - dimension over which to concatenate [default - both]
0006 %__________________________________________________________________________
0007 % Empty array elements are replaced by sparse zero partitions
0008 % and single 0 entries are expanded to conform to the non-empty
0009 % non zero elements.
0010 %
0011 % e.g.:
0012 % > x       = pr_spm_cat({eye(2) []; 0 [1 1; 1 1]})
0013 % > full(x) =
0014 %
0015 %     1     0     0     0
0016 %     0     1     0     0
0017 %     0     0     1     1
0018 %     0     0     1     1
0019 %__________________________________________________________________________
0020 % Copyright (C) 2005 Wellcome Department of Imaging Neuroscience
0021 
0022 % Karl Friston
0023 % $Id: spm_cat.m 258 2005-10-18 18:21:07Z karl $
0024  
0025 % check x is not already a matrix
0026 %--------------------------------------------------------------------------
0027 if ~iscell(x), return, end
0028  
0029 % if concatenation over a specific dimension
0030 %--------------------------------------------------------------------------
0031 [n m] = size(x);
0032 if nargin > 1
0033  
0034     % concatenate over first dimension
0035     %----------------------------------------------------------------------
0036     if d == 1
0037         y = cell(1,m);
0038         for i = 1:m
0039             y{i} = pr_spm_cat(x(:,i));
0040         end
0041  
0042     % concatenate over second
0043     %----------------------------------------------------------------------
0044     elseif d == 2
0045  
0046         y = cell(n,1);
0047         for i = 1:n
0048             y{i} = pr_spm_cat(x(i,:));
0049         end
0050  
0051     % only viable for 2-D arrays
0052     %----------------------------------------------------------------------
0053     else
0054         error('uknown option')
0055     end
0056     x      = y;
0057     return
0058  
0059 end
0060  
0061 % find dimensions to fill in empty partitions
0062 %--------------------------------------------------------------------------
0063 for i = 1:n
0064 for j = 1:m
0065     if iscell(x{i,j})
0066         x{i,j} = pr_spm_cat(x{i,j});
0067     end
0068     [u v]  = size(x{i,j});
0069     I(i,j) = u;
0070     J(i,j) = v;
0071 end
0072 end
0073 I     = max(I,[],2);
0074 J     = max(J,[],1);
0075  
0076 % sparse and empty partitions
0077 %--------------------------------------------------------------------------
0078 [n m] = size(x);
0079 for i = 1:n
0080 for j = 1:m
0081     if isempty(x{i,j})
0082         x{i,j} = zeros(I(i),J(j));
0083     elseif ~x{i,j}
0084         x{i,j} = zeros(I(i),J(j));
0085     else
0086         x{i,j} = full(x{i,j});
0087     end
0088 end
0089 end
0090  
0091 % concatenate
0092 %--------------------------------------------------------------------------
0093 for i = 1:n
0094     y{i,1} = cat(2,x{i,:});
0095 end
0096 x     = sparse(cat(1,y{:}));

Generated on Wed 11-May-2022 16:26:09 by m2html © 2003-2019