Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
how to set loop structure of uncertain layer for exhaustion permutation

Subject: how to set loop structure of uncertain layer for exhaustion permutation

From: lily

Date: 19 Apr, 2012 02:27:31

Message: 1 of 3

Hi, everyone.
I want to get all the permutations of C(n,k), in which "n" is the sample space, "k" is the size of sampling each time. For example, I set the loop structure for C(5,3) as follow:
clear;clc;
n=3;k=2;y=[];cnt=0;
for i=1:n
    for i2=1:n
        for i3=1:n
            tmp=[i i2 i3];
            if length(unique(tmp))==3 %% make sure no duplicates in [i i2 i3].
                cnt=cnt+1;
                y(cnt,1:3)=tmp; %% y is the permutation of C(n,k).
            end
        end
    end
end
My question is that, "k" is variable or uncertain, then how to set loop structure of uncertain layer i"k". Is there any way to fix this problem, nested structure? Any help is greatly appreciated.

Subject: how to set loop structure of uncertain layer for exhaustion permutation

From: Roger Stafford

Date: 19 Apr, 2012 06:28:13

Message: 2 of 3

"lily" wrote in message <jmnt6j$ev2$1@newscl01ah.mathworks.com>...
> I want to get all the permutations of C(n,k), in which "n" is the sample space, "k" is the size of sampling each time. ......
- - - - - - - - - - -
 t = nchoosek(1:n,k);
 nc = size(t,1);
 nk = prod(1:k);
 y = zeros(nc*nk,k);
 for m = 1:nc
   y(1+nk*(m-1):nk*m,:) = perms(t(m,:));
 end

Roger Stafford

Subject: how to set loop structure of uncertain layer for exhaustion permutation

From: lily

Date: 24 Apr, 2012 01:03:08

Message: 3 of 3

"Roger Stafford" wrote in message <jmob9t$9e7$1@newscl01ah.mathworks.com>...
> "lily" wrote in message <jmnt6j$ev2$1@newscl01ah.mathworks.com>...
> > I want to get all the permutations of C(n,k), in which "n" is the sample space, "k" is the size of sampling each time. ......
> - - - - - - - - - - -
> t = nchoosek(1:n,k);
> nc = size(t,1);
> nk = prod(1:k);
> y = zeros(nc*nk,k);
> for m = 1:nc
> y(1+nk*(m-1):nk*m,:) = perms(t(m,:));
> end
>
> Roger Stafford

Thanks Roger very much, and I find the command "t=combntns(n,k)" can get all the combinations of C(n,k) too.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us