Results 1 to 4 of 4

Math Help - Permutation function in MATLAB

  1. #1
    Newbie
    Joined
    Jan 2009
    Posts
    21

    Permutation function in MATLAB

    First, I am in an introductory level course, so I have to use for loops in this function. I've been tinkering with the code for a couple hours now and can't get on the right track.

    What I've got to do is, have a function with a parameter that creates a permutation of those numbers, i.e.

    L = function(3)

    L =
    [ 1 3 2
    1 2 3
    2 1 3
    2 3 1
    3 1 2
    3 2 1
    ]


    and L is pre-created as zeros such that

    L = zeros(factorial(n),3);


    and then within our loops,

    we have something like L(row,:) = [ assignment ]

    Any help is greatly appreciated.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by mjlaz View Post
    First, I am in an introductory level course, so I have to use for loops in this function. I've been tinkering with the code for a couple hours now and can't get on the right track.

    What I've got to do is, have a function with a parameter that creates a permutation of those numbers, i.e.

    L = function(3)

    L =
    [ 1 3 2
    1 2 3
    2 1 3
    2 3 1
    3 1 2
    3 2 1
    ]


    and L is pre-created as zeros such that

    L = zeros(factorial(n),3);


    and then within our loops,

    we have something like L(row, = [ assignment ]

    Any help is greatly appreciated.

    A basic function with a loop for this looks like:

    Code:
    function rv=permutations(n)
    
       prm0=[1:n]
       rv=zeros(gamma(n-1),n);
       rv(1,:)=prm0;
    
       for idx=2 : gamma(n)-1
       
    
    %
    %     put caluculation for next permutation here
    %
    
       end
    if you want us to be more specific tell us what algorithm you want to use to generate the permutations.

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Jan 2009
    Posts
    21
    Thanks....

    I've worked on it this morning and I have this:
    Code:
    function L = perms(n)
        row = 1;
        Z = zeros(factorial(n),3);
        
        for i=1:n
            for j=2:n
                for k=3:n
                    Z(row,:) = [i,j,k];
                    row = row + 1;
                end
            end
        end
    end
    And it gives
    Code:
    perms3(3)
    
    ans =
    
         1     2     3
         1     3     3
         2     2     3
         2     3     3
         3     2     3
         3     3     3
    What I need is
    Code:
    ans =
    
         1     2     3
         1     3     2
         2     1     3
         2     3     1
         3     1     2
         3     2     1
    So, I'm close, but no cigar yet. My methods may seem cumbersome, but I'm having to follow others guidelines.

    Any more ideas? Your taking the time to help is appreciated!
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by mjlaz View Post
    Thanks....

    I've worked on it this morning and I have this:
    Code:
    function L = perms(n)
        row = 1;
        Z = zeros(factorial(n),3);
        
        for i=1:n
            for j=2:n
                for k=3:n
                    Z(row,:) = [i,j,k];
                    row = row + 1;
                end
            end
        end
    end
    And it gives
    Code:
    perms3(3)
    
    ans =
    
         1     2     3
         1     3     3
         2     2     3
         2     3     3
         3     2     3
         3     3     3
    What I need is
    Code:
    ans =
    
         1     2     3
         1     3     2
         2     1     3
         2     3     1
         3     1     2
         3     2     1
    So, I'm close, but no cigar yet. My methods may seem cumbersome, but I'm having to follow others guidelines.

    Any more ideas? Your taking the time to help is appreciated!
    Try this

    Code:
    function L = perms()
        row = 1; n=3
        Z = zeros(factorial(n),3);
        idxarray1=[1:n]
        for idx=idxarray1
            idxarray2=idxarry1(find(idxarray1~=idx))   
            for jdx=idxarry2
                idxarray3=idxarry2(find(idxarray2~=jdx)) 
                for kdx=idxarray3
                    Z(row,:) = [idx,jdx,kdx];
                    row = row + 1;
                end
            end
        end
    end
    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 3
    Last Post: September 21st 2011, 05:23 PM
  2. Matlab function
    Posted in the Math Software Forum
    Replies: 2
    Last Post: April 17th 2010, 08:11 PM
  3. Please help with MATLAB function
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 16th 2010, 04:21 AM
  4. is there any function in MATLAB?
    Posted in the Math Software Forum
    Replies: 1
    Last Post: April 4th 2010, 01:57 AM
  5. Matlab while function
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 9th 2009, 11:46 PM

Search Tags


/mathhelpforum @mathhelpforum