spm_cat.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. function [x] = spm_cat(x,d)
  2. % Convert a cell array into a matrix - a compiled routine
  3. % FORMAT [x] = spm_cat(x,d)
  4. % x - cell array
  5. % d - dimension over which to concatenate [default - both]
  6. %__________________________________________________________________________
  7. % Empty array elements are replaced by sparse zero partitions and single 0
  8. % entries are expanded to conform to the non-empty non zero elements.
  9. %
  10. % e.g.:
  11. % > x = spm_cat({eye(2) []; 0 [1 1; 1 1]})
  12. % > full(x) =
  13. %
  14. % 1 0 0 0
  15. % 0 1 0 0
  16. % 0 0 1 1
  17. % 0 0 1 1
  18. %
  19. % If called with a dimension argument, a cell array is returned.
  20. %__________________________________________________________________________
  21. % Copyright (C) 2005-2013 Wellcome Trust Centre for Neuroimaging
  22. % Karl Friston
  23. % $Id: spm_cat.m 5731 2013-11-04 18:11:44Z guillaume $
  24. %error('spm_cat.c not compiled - see Makefile')
  25. % check x is not already a matrix
  26. %--------------------------------------------------------------------------
  27. if ~iscell(x), return, end
  28. % if concatenation over a specific dimension
  29. %--------------------------------------------------------------------------
  30. [n,m] = size(x);
  31. if nargin > 1
  32. % concatenate over first dimension
  33. %----------------------------------------------------------------------
  34. if d == 1
  35. y = cell(1,m);
  36. for i = 1:m
  37. y{i} = spm_cat(x(:,i));
  38. end
  39. % concatenate over second
  40. %----------------------------------------------------------------------
  41. elseif d == 2
  42. y = cell(n,1);
  43. for i = 1:n
  44. y{i} = spm_cat(x(i,:));
  45. end
  46. % only viable for 2-D arrays
  47. %----------------------------------------------------------------------
  48. else
  49. error('uknown option')
  50. end
  51. x = y;
  52. return
  53. end
  54. % find dimensions to fill in empty partitions
  55. %--------------------------------------------------------------------------
  56. for i = 1:n
  57. for j = 1:m
  58. if iscell(x{i,j})
  59. x{i,j} = spm_cat(x{i,j});
  60. end
  61. [u,v] = size(x{i,j});
  62. I(i,j) = u;
  63. J(i,j) = v;
  64. end
  65. end
  66. I = max(I,[],2);
  67. J = max(J,[],1);
  68. % sparse and empty partitions
  69. %--------------------------------------------------------------------------
  70. [n,m] = size(x);
  71. for i = 1:n
  72. for j = 1:m
  73. if isempty(x{i,j})
  74. x{i,j} = sparse(I(i),J(j));
  75. end
  76. end
  77. end
  78. % concatenate
  79. %--------------------------------------------------------------------------
  80. for i = 1:n
  81. y{i,1} = cat(2,x{i,:});
  82. end
  83. try
  84. x = sparse(cat(1,y{:}));
  85. catch
  86. x = cat(1,y{:});
  87. end