The generation of matlab combination sequence, bijection function of sequence and sequence number

Keywords: MATLAB

Label (space separated): matlab combination algorithm mapping

The generation of matlab combination sequence, bijection function of sequence and sequence number

1.1 generating combined sequences

% Functions of the system
>> nchoosek(1:4, 3)
ans =
     1     2     3
     1     2     4
     1     3     4
     2     3     4
% Functions I wrote
>> myChoose(4, 3)
ans =
     1     2     3
     1     2     4
     1     3     4
     2     3     4

The nchoosek() and myChoose() functions of the system can generate all combinations, but the chooseContinue() function called by myChoose() can generate combination sequences one by one, which can save memory. For example:

>> n=4; r=3;
seq =[1:r]
[seq,loopJudge]=chooseContinue(n,r,seq)
seq =
     1     2     3
seq =
     1     2     4
loopJudge =
     1
>> [seq,loopJudge]=chooseContinue(n,r,seq)
seq =
     1     3     4
loopJudge =
     1
>> [seq,loopJudge]=chooseContinue(n,r,seq)
seq =
     2     3     4
loopJudge =
     1
>> [seq,loopJudge]=chooseContinue(n,r,seq)
seq =
     []
loopJudge =
     0

1.2 mapping of composite sequences to ordinals

>> r=3;
>> sequence2num(r,[1 2 3])
ans =
     1
>> sequence2num(r,[1 2 4])
ans =
     2
>> sequence2num(r,[1 3 4])
ans =
     3
>> sequence2num(r,[2 3 4])
ans =
     4

1.3 ordinal mapping to composite sequences

>> n=4; r=3;
>> seq= num2sequence(n,r,1)
seq =
     1     2     3
>> seq= num2sequence(n,r,2)
seq =
     1     2     4
>> seq= num2sequence(n,r,3)
seq =
     1     3     4
>> seq= num2sequence(n,r,4)
seq =
     2     3     4

1.4 source code

1.4.1 myChoose()

function seqs=myChoose(n,r)
%Generate the main function of [generate all combinations one by one]
%% Original algorithm
%Time complexity is O(C(n,r)*r),The space complexity is O(C(n,r)*r)
    %However, when it is only necessary to return the combined sequences one by one (usually satisfied), the spatial complexity is O(r)
%Time: 2013/12/28 15:28  Name: Deng nengcai   
%%
seqs=zeros(nchoosek(n,r),r);
loopJudge=true; i=1;
seqs(i,:)=[1:r];
while true
    [seqTemp,loopJudge]=chooseContinue(n,r,seqs(i,:));
    if loopJudge
        seqs(i+1,:)=seqTemp;
        i=i+1;
    else break; end
end
end

1.4.2 chooseContinue()

function [seq,loopJudge]=chooseContinue(n,r,seq)
% Subfunction: generate next combination
%Time: 2013/12/28 15:28  Name: Deng nengcai   
%Find the child that can move forward
i=1;
while i+1<=r
    if seq(i+1)==seq(i)+1
        i=i+1;
    else  break; end
end
if i==r && seq(r)==n %When the last sequence has been reached
    seq=[]; loopJudge=false;
else 
    seq(i)=seq(i)+1; %Section i Move forward
    for j=1:i-1, seq(j)=j; end %Move back to home position
    loopJudge=true;
end
end

1.4.3 sequence2num()

function num= sequence2num(r,seq)
%Map [combination sequence] to C(n,r)Of all combinations of
%% Original algorithm
%Time: 2013/12/27 22:33  Name: Deng nengcai   
%%
num=0;
for i=r:-1:1
    if seq(i)==i, break; end
    num=num+nchoosek(seq(i)-1,i);
end
num=num+1;
end

% % % %Theoretical basis: Combinatorial Mathematics((U.S.A)Richard A.Brualdi)
% % % num=nchoosek(n,r);
% % % seq
% % % for i=1:r
% % %     ni=n-seq(i),ri=r-i+1
% % %     if ni~=0
% % %         num=num-nchoosek(ni,ri);
% % %     else
% % %         break;
% % %     end
% % % end
% % % end   [Not using the theory]

1.4.4 num2sequence()

function seq= num2sequence(n,r,num)
%take C(n,r)The sequence number of a combination of is mapped to the combination sequence
%% Original algorithm
%When [pre calculated C(j,i)Matrix,
    %Time complexity is O(n*r),The space complexity is O(n*r)
%Time: 2013/12/28 15:28  Name: Deng nengcai   
%%
seq=[1:r];
lastPoint=n;
num=num-1;
for i=r:-1:1
    % '##loop i'
    if num==0, break; end
    for j=lastPoint:-1:1
        % j_i=[j,i]   % ( i==j || 
        if num<nchoosek(j,i) && num>=nchoosek(j-1,i)
            seq(i)=j; lastPoint=j-1; break;
        end
    end
    num=num-nchoosek(j-1,i);
end
end

Contact author definedone@163.com

end

Posted by Daniello on Sat, 21 Dec 2019 12:47:22 -0800