Professional Documents
Culture Documents
% History
% 1.1 updated help text
% 2.0 new faster algorithm
% 3.0 (aug 2006) implemented very fast algorithm
% 3.1 (may 2007) Improved algorithm Roger Stafford pointed out that for some val
ues, the floor
% operation on floating points, according to the IEEE 754 standard, could return
% erroneous values. His excellent solution was to add (1/2) to the values
% of A.
% 3.2 (may 2007) changed help and error messages slightly
% 4.0 (may 2008) again a faster implementation, based on ALLCOMB, suggested on t
he
%
newsgroup comp.soft-sys.matlab on May 7th 2008 by "Helper". It was
%
pointed out that COMBN(V,N) equals ALLCOMB(V,V,V...) (V repeated N
%
times), ALLCMOB being faster. Actually version 4 is an improvement
%
over version 1 ...
% 4.1 (jan 2010) removed call to FLIPLR, using refered indexing N:-1:1
%
(is faster, suggestion of Jan Simon, jan 2010), removed REPMAT, and
%
let NDGRID handle this
% 4.2 (apr 2011) corrrectly return a column vector for N = 1 (error pointed
%
out by Wilson).
error(nargchk(2,2,nargin)) ;
if isempty(V) || N == 0,
M = [] ;
IND = [] ;
elseif fix(N) ~= N || N < 1 || numel(N) ~= 1 ;
%
%
%
%
%
%
%
%
%
%
%
%
%
%
can be implemented in C or VB
nv = length(V) ;
C = zeros(nv^N,N) ; % declaration
for ii=1:N,
cc = 1 ;
for jj=1:(nv^(ii-1)),
for kk=1:nv,
for mm=1:(nv^(N-ii)),
C(cc,ii) = V(kk) ;
cc = cc + 1 ;
end
end
end
end