# Math Help - Permutation function in MATLAB

1. ## 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.

2. Originally Posted by mjlaz
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

3. 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!

4. Originally Posted by mjlaz
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