-
Notifications
You must be signed in to change notification settings - Fork 3
/
spm_orth.m
63 lines (59 loc) · 1.58 KB
/
spm_orth.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
function X = spm_orth(X,OPT)
% Recursive Gram-Schmidt orthogonalisation of basis functions
% FORMAT X = spm_orth(X,OPT)
%
% X - matrix
% OPT - 'norm' for Euclidean normalisation
% - 'pad' for zero padding of null space [default]
%
% Serial orthogonalisation starting with the first column
%
% Reference:
% Golub, Gene H. & Van Loan, Charles F. (1996), Matrix Computations (3rd
% ed.), Johns Hopkins, ISBN 978-0-8018-5414-9.
%__________________________________________________________________________
% Copyright (C) 2002-2012 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_orth.m 8045 2021-02-02 18:46:28Z karl $
# SPDX-License-Identifier: GPL-2.0
%-Default
%--------------------------------------------------------------------------
try
OPT;
catch
OPT = 'pad';
end
%-Recursive Gram-Schmidt orthogonalisation
%--------------------------------------------------------------------------
sw = warning('off','all');
[n,m] = size(X);
X = full(X(:, any(X)));
rankX = rank(X);
try
x = X(:,1);
j = 1;
for i = 2:size(X, 2)
D = X(:,i);
D = D - x*(pinv(x)*D);
if norm(D,1) > exp(-32)
x = [x D];
j(end + 1) = i;
end
if numel(j) == rankX, break, end
end
catch
x = zeros(n,0);
j = [];
end
warning(sw);
% and normalisation, if requested
%--------------------------------------------------------------------------
switch OPT
case{'pad'}
X = zeros(n,m);
X(:,j) = x;
case{'norm'}
X = spm_en(x);
otherwise
X = x;
end