-
Notifications
You must be signed in to change notification settings - Fork 3
/
spm_perm_mtx.m
52 lines (46 loc) · 1.42 KB
/
spm_perm_mtx.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
function [K] = spm_perm_mtx(n)
% Returns a matrix of indices permuted over n
% FORMAT [K] = spm_perm_mtx(n)
% n - (scalar) number of indices
% K - (2^n x n) permutation matrix
% n - (vector) indices
% K - (length(n)! x n) permutation matrix
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_perm_mtx.m 5657 2013-09-26 16:53:40Z karl $
# SPDX-License-Identifier: GPL-2.0
% get permutations
%==========================================================================
% permute zeros and ones
%--------------------------------------------------------------------------
if isscalar(n)
N = 2^n;
K = sparse(N,n);
x = sparse(1,1,1,2,1);
for i = 1:n
y = ones(N/length(x),1);
K(:,i) = kron(x,y);
x = [x;x];
end
% permute indices
%--------------------------------------------------------------------------
elseif isvector(n)
n = n(:);
K = n;
while size(K,2) < length(n)
x = [];
for i = 1:size(K,1)
d = K(i,:);
r = n;
for j = 1:length(d)
r(r == d(j)) = [];
end
x = [x; [kron(ones(length(r),1),d) r]];
end
K = x;
end
end
% make logical
%--------------------------------------------------------------------------
K = logical(K);