Let's say I have an n x p matrix with columns [a_1, ... , a_p]. How can I use a for-loop to return all possible column permutations of this matrix (for example: **[a_2,a_1,...,a_p] etc. **)?
            Asked
            
        
        
            Active
            
        
            Viewed 212 times
        
    -5
            
            
        - 
                    2Check out this other question: [Generate all possible combinations of the elements of some vectors (Cartesian product)](http://stackoverflow.com/questions/4165859/generate-all-possible-combinations-of-the-elements-of-some-vectors-cartesian-pr) – Schorsch Oct 20 '15 at 12:44
 
1 Answers
1
            If I understand correctly, you can generate all permutations of column indices with perms, and use the result to index into the matrix:
M = magic(3);              % // example matrix
pp = perms(1:size(M,2));   % // each row is a permutation
for p = pp.'               %'// "for" loops over columns, so transpose pp
    permuted_M = M(:,p);   % // matrix with permuted columns
end
        Luis Mendo
        
- 110,752
 - 13
 - 76
 - 147