File: CombVec.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (49 lines) | stat: -rw-r--r-- 1,291 bytes parent folder | download | duplicates (6)
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
function out = CombVec(varargin)
%CombVec Generate all possible combinations of input vectors.
%
%   CombVec(A1,A2,...) takes any number of inputs,
%      A1 - Matrix of N1 (column) vectors.
%      A2 - Matrix of N2 (column) vectors.
%      ...
%    and returns a matrix of (N1*N2*...) column vectors, where the columns
%    consist of all possibilities of A2 vectors, appended to
%    A1 vectors, etc.
%
%  Example
%  
%    a1 = [1 2];
%    a2 = [3 4; 3 4];
%    a3 = CombVec(a1,a2)
%    a3 = 
%        1     2     1     2
%        3     3     4     4
%        3     3     4     4

% 2008-08-06 DN  Wrote it, modification of CombVec in Matlab's Neural
%                Network Toolbox

if isempty(varargin)
    out = [];
else
    out = varargin{1};
    for i=2:length(varargin)
        cur = varargin{i};
        out = [copyb(out,size(cur,2)); copyi(cur,size(out,2))];
    end
end

%=========================================================
function b = copyb(mat,s)

[mr,mc] = size(mat);
inds    = 1:mc;
inds    = inds(ones(s,1),:).';
b       = mat(:,inds(:));

%=========================================================
function b = copyi(mat,s)

[mr,mc] = size(mat);
inds    = 1:mc;
inds    = inds(ones(s,1),:);
b       = mat(:,inds(:));