-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspm_cat.m
100 lines (88 loc) · 2.61 KB
/
spm_cat.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function [x] = spm_cat(x,d)
% Convert a cell array into a matrix - a compiled routine
% FORMAT [x] = 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 = 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
%
% If called with a dimension argument, a cell array is returned.
%__________________________________________________________________________
% Copyright (C) 2005-2013 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_cat.m 8045 2021-02-02 18:46:28Z karl $
# SPDX-License-Identifier: GPL-2.0
%error('spm_cat.c not compiled - see Makefile')
% check x is not already a matrix
%--------------------------------------------------------------------------
if ~iscell(x), return, end
% if concatenation over a specific dimension
%--------------------------------------------------------------------------
[n,m] = size(x);
if nargin > 1
% concatenate over first dimension
%----------------------------------------------------------------------
if d == 1
y = cell(1,m);
for i = 1:m
y{i} = spm_cat(x(:,i));
end
% concatenate over second
%----------------------------------------------------------------------
elseif d == 2
y = cell(n,1);
for i = 1:n
y{i} = spm_cat(x(i,:));
end
% only viable for 2-D arrays
%----------------------------------------------------------------------
else
error('uknown option')
end
x = y;
return
end
% find dimensions to fill in empty partitions
%--------------------------------------------------------------------------
for i = 1:n
for j = 1:m
if iscell(x{i,j})
x{i,j} = spm_cat(x{i,j});
end
[u,v] = size(x{i,j});
I(i,j) = u;
J(i,j) = v;
end
end
I = max(I,[],2);
J = max(J,[],1);
% sparse and empty partitions
%--------------------------------------------------------------------------
[n,m] = size(x);
for i = 1:n
for j = 1:m
if isempty(x{i,j})
x{i,j} = sparse(I(i),J(j));
end
end
end
% concatenate
%--------------------------------------------------------------------------
for i = 1:n
y{i,1} = cat(2,x{i,:});
end
try
x = sparse(cat(1,y{:}));
catch
x = cat(1,y{:});
end