combinations.m

来自「模式分类(杜达)(第二版)电子书 及原书中的代码。绝对值得永久收藏。」· M 代码 · 共 33 行

M
33
字号
function indices = combinations (in_from, N)

% Find all N combinations of input indices 
%
% Inputs:
%   in_from - The input indices
%   N       - How many indices are requiered
%
% Outputs:
%   indices - The indice combinations

Nf = length(in_from);

comb_mat  = zeros(2^Nf, Nf);
basic_mat = [0, 1];

for i = 1:Nf,
    rep_mat     = ones(2^(i-1), 1) * basic_mat;
    pattern     = rep_mat(:);
    rep_pattern = pattern * ones(1, 2^(Nf-i));
    column      = rep_pattern(:);
    comb_mat(:,Nf - i + 1) = column;
end

%Find the number of the right rows
in = find(sum(comb_mat') == N);

indices = zeros(length(in), N);
for i = 1:length(in),
    cur_in = find(comb_mat(in(i), :));
    indices(i, :) = in_from(cur_in);
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?