combinations.m
来自「Duda的《模式分类》第二版的配套的Matlab源代码」· 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 + -
显示快捷键?