views:

258

answers:

2
+2  Q: 

Matlab swapping

Hello, I'm trying to program a matlab to list all permutations of the numbers 1 through n in lexicographical order. What I have so far is below. I am using recursion to try and write a program that will work for n=3 first, and then see if I can gain insight into writing the program for any n. So far I have 2 of the 6 columns for n=3: P=[1 2 3;1 3 2]. I need the next two columns to simply swap the ones and the twos. I dont know how to begin to do that. I appreciate any help. Thanks.

function [P] = shoes(n)

if n == 1
    P = 1;
elseif n == 2
    P = [1 2; 2 1];
else 
    T = shoes(n-1) + 1;
    G = ones(factorial(n-1),1);
    P(1:2,1:3) = [G T];               
end
+1  A: 

See the documentation for a start. If by lexicographical order you mean alphabetical by english name, you may want to populate your input with the names, sort them, then permute that.

If I've misunderstood what you're wanting, comment or edit the question & I'll check back later.

Hints:

  • The permutations of an empty list are easy to find.
  • Induction is an important concept in mathematics. You should be familiar with it.
  • The environment you are working in supports recursion
  • The permutations of a longer list can be produced in the order you want by recursion; first figure out what you want the first element to be, and then figure out the rest.

If you get stuck again, edit the question posting what you've gotten so far and where/why you think you're stuck.

Hints after seeing your code.

  • Your core function permutes a vector, and so should take a vector as argument, not an integer
  • Don't start solving the n=3 case; try the n=0 case (it's []) and then go straight to the n=20 case.
  • Think about the n=20 case before you write any code. What is the first column going to look like? Are there any examples of the n=19 case hidden in the answer to the n=20 case? (The answer is yes, and they are all different).
  • Reread the first set of hints
MarkusQ
Hello and thanks for the quick reply. Actually, I'm not allowed to use the built in function perms. And to xlarify, what I need to do is if n=3 for example, the output needs to be exactly A = [1 2 3;1 3 2;2 1 3;2 3 1;3 1 2;3 2 1]. Is this any more clear?
Mark it with the "homework" tag and I'll check back later. Meanwhile, I'll add a hint.
MarkusQ
+1  A: 

You appear to have asked this question twice. Instead of reposting questions, you should simply click the "edit" link below your question and update it. I'll repost here the answer I gave to your other question, but you should really remove one of them.


If you have the following matrix:

A = [1 2 3; 1 3 2];

and you want all the ones to become twos and the twos to become ones, the following would be the simplest way to do it:

B = A;
B(A == 1) = 2;
B(A == 2) = 1;
gnovice
Hi I just wanna say thanks so much for your help. This one answer enabled me to write the entire program. I really appreciate it.
Glad I could help. =)
gnovice

related questions