-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakematrices.m
191 lines (173 loc) · 5.67 KB
/
makematrices.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
%==========================================================================
% Recursively walks through a Matlab hierarchy and substitutes cell vectors
% by a matrix when possible.
% Specifically substitutes cell objects like
%
% {{1,2,3},{4,5,6}}
%
% by
%
% {1,2,3;4,5,6}
%
% It leaves other objects unchanged except that it may change cell
% orientations (from column to row, etc.)
%
% Parameter makeords determines whether to convert from cells to normal
% matrices whenever possible (1) or leave matrices as cells (0).
%==========================================================================
function result = makematrices(r, makeords)
result = recurse(r, 0, [], makeords);
end
%--------------------------------------------------------------------------
%
%
function result = recurse(data, level, addit, makeords)
if iscell(data)
result = iter_cell(data, level, addit, makeords);
elseif isstruct(data)
result = iter_struct(data, level, addit, makeords);
else
result = scan_data(data, level, addit);
end;
end
%--------------------------------------------------------------------------
% Iterates through cell array data. A cell array here is treated as a
% simple sequence, hence it is processed regardless its shape. The array is
% transformed to a cell matrix if it satisfies following conditions:
% - It is vector
% - All its items are cells
% - All its items are vectors
% - All its items are alligned (are of the same size)
% - All its items are rows of a matrix (see ismatrixrow(...))
% Otherwise the content is left unchanged.
%
function result = iter_cell(data, level, addit, makeords)
if isvector(data) && ...
iscell_all(data) && ...
isvector_all(data) && ...
isaligned_all(data) && ...
ismatrixrow_all(data)
tmp = data;
tmp = cellfun(@cell2mat, tmp, 'UniformOutput', 0);
tmp = cellfun(@torow, tmp, 'UniformOutput', 0);
tmp = tocolumn(tmp);
tmp = cell2mat(tmp);
if ~makeords
tmp = num2cell(tmp);
end;
result = tmp;
elseif isempty(data)
result = [];
else
result = {};
for i = 1:length(data)
result{i} = recurse(data{i}, level + 1, addit, makeords);
end;
end;
end
%--------------------------------------------------------------------------
%
%
function result = iter_struct(data, level, addit, makeords)
result = struct();
for i = fields(data)'
fld = char(i);
result.(fld) = recurse(data.(fld), level + 1, addit, makeords);
end;
end
%--------------------------------------------------------------------------
%
%
function result = scan_data(data, level, addit)
result = data;
end
%--------------------------------------------------------------------------
%
%
function result = iscell_all(cellvec)
result = all(cellfun(@iscell, cellvec));
end
%--------------------------------------------------------------------------
% Determines whether all items of cellvec are of the same length.
%
function result = isaligned_all(cellvec)
siz = numel(cellvec{1});
result = all(cellfun(@numel, cellvec) == siz);
end
%--------------------------------------------------------------------------
%
%
function result = ismatrixrow_all(cellvec)
result = all(cellfun(@ismatrixrow, cellvec));
end
%--------------------------------------------------------------------------
% Determines whether cellvec can constitute a matrix row. The vector is a
% matrix row candidate if:
% - all of its items are numeric
% - all of its items are single (neither vectors nor matrices etc.)
% - all of its items are compatible for concatenation to an ordinary
% vector (this is maybe automatically reached by isnumeric_all)
%
function result = ismatrixrow(cellvec)
result = ...
(isnumeric_all(cellvec) || islogical_all(cellvec) || isstruct_all(cellvec)) && ...
issingle_all(cellvec) && ...
iscompatible_all(cellvec);
end
%--------------------------------------------------------------------------
%
%
function result = isnumeric_all(cellvec)
result = all(cellfun(@isnumeric, cellvec));
end
%--------------------------------------------------------------------------
%
%
function result = islogical_all(cellvec)
result = all(cellfun(@islogical, cellvec));
end
%--------------------------------------------------------------------------
%
%
function result = issingle_all(cellvec)
result = all(cellfun(@issingle, cellvec));
end
%--------------------------------------------------------------------------
%
%
function result = iscompatible_all(cellvec)
result = true;
for i = 1:(length(cellvec) - 1)
result = result && iscompatible(cellvec{i}, cellvec{i + 1});
end
end
%--------------------------------------------------------------------------
%
%
function result = iscompatible(obj1, obj2)
result = isequal(class(obj1), class(obj2));
end
%--------------------------------------------------------------------------
%
%
function result = isvector_all(cellvec)
result = all(cellfun(@isvector, cellvec));
end
%--------------------------------------------------------------------------
%
%
function result = isstruct_all(cellvec)
result = all(cellfun(@isstruct, cellvec));
end
%--------------------------------------------------------------------------
%
%
function result = torow(vec)
result = tocolumn(vec).';
end
%--------------------------------------------------------------------------
%
%
function result = tocolumn(vec)
result = vec(:);
end