-
Notifications
You must be signed in to change notification settings - Fork 3
/
spm_length.m
50 lines (44 loc) · 1.35 KB
/
spm_length.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
function [n] = spm_length(X)
% Length of a vectorised numeric, cell or structure array
% FORMAT [n] = spm_length(X)
% X - numeric, cell or stucture array[s]
% n - length(spm_vec(X))
%
% See spm_vec, spm_unvec
%__________________________________________________________________________
%
% e.g.:
% spm_length({eye(2) 3}) = 5
%__________________________________________________________________________
% Copyright (C) 2014 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_length.m 6233 2014-10-12 09:43:50Z karl $
# SPDX-License-Identifier: GPL-2.0
% vectorise numerical arrays
%--------------------------------------------------------------------------
if isnumeric(X)
n = numel(X);
% vectorise logical arrays
%--------------------------------------------------------------------------
elseif islogical(X)
n = numel(X);
% vectorise structure into cell arrays
%--------------------------------------------------------------------------
elseif isstruct(X)
n = 0;
f = fieldnames(X);
for i = 1:numel(f)
for j = 1:numel(X)
n = n + spm_length(X(j).(f{i}));
end
end
% vectorise cells into numerical arrays
%--------------------------------------------------------------------------
elseif iscell(X)
n = 0;
for i = 1:numel(X)
n = n + spm_length(X{i});
end
else
n = 0;
end