forked from pgagarinov/spheretri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
combvec.m
60 lines (60 loc) · 1.6 KB
/
combvec.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
function indMat = combvec(varargin)
% COMBVEC creates a matrix of combinations with elements from input vectors
%
% Usage: indMat=combvec(firstVec,secVec,...)
%
% Input:
% optional:
% firstVec: numeric[1,n1Elems] - first vector
% ...
% lastVec: numeric[1,nKElems] - last vector
%
% Output:
% indMat: numeric[K,nCombs] - matrix with combinations from input vectors
%
% Example:
%
% firstVec=[1,2,3];
% secVec=[4,5];
% indMat=mxberry.core.combvec(firstVec,secVec);
% indMat
%
% indMat =
%
% 1 2 3 1 2 3
% 4 4 4 5 5 5
%
% $Author: Peter Gagarinov, PhD <[email protected]> $
% $Copyright: Peter Gagarinov, PhD,
% Moscow State University,
% Faculty of Computational Mathematics and Computer Science,
% System Analysis Department 2011-2016 $
%
nInputs=nargin;
if nInputs==0
indMat = [];
else
indMat = varargin{1};
for iInp=2:nInputs
curVec = varargin{iInp};
indMat = [addBlockedInd(indMat,size(curVec,2));...
addInterleavedInd(curVec,size(indMat,2))];
end
end
%
function indResMat = addBlockedInd(indMat,nElems)
[nDims,nCombs] = size(indMat);
indResMat = zeros(nDims,nCombs*nElems);
indVec = 1:nCombs;
for iCombToAdd=(0:(nElems-1))*nCombs
indResMat(:,indVec+iCombToAdd) = indMat;
end
%
function indResMat = addInterleavedInd(indMat,nElems)
[nDims,nCombs] = size(indMat);
indResMat = zeros(nDims*nElems,nCombs);
ind = 1:nDims;
for iCombToAdd=(0:(nElems-1))*nDims
indResMat(ind+iCombToAdd,:) = indMat;
end
indResMat = reshape(indResMat,nDims,nElems*nCombs);