# Math Help - Fast algorithm for calculating permutations

1. ## Fast algorithm for calculating permutations

I was wondering where I could find fast algorithms for calculating permutations. I tried using a matlab built in code perms() and it calculates a matrix of values. I would like to output each permutation individually (i.e. in a for loop) so i can perform some manipulations on them. Thanks in advance.

MT

2. You just need to iterate over your matrix. I'm using GNU Octave, but it's very close to MatLab so the same should work for you.

Let's say you want the permutations of some matrix such as this:

Code:
a = [1, 2, 3, 4]
Make the permutation matrix:

Code:
b = perms(a)
Now you can iterate over it with something like:

Code:
for j = 1:size(b); b(j,:) endfor
That's just printing the rows one by one (because I didn't put a semi-colon after the
Code:
b(j,:)
but you can process each row as you go. Just do something right before the endfor.

It's worthwhile to really get to know how to manipulate matrices in MatLab. That's pretty much how you do everything in it.