Swapping the columns of a matrix with every other column
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Robert Elston
am 17 Mai 2020
Kommentiert: Robert Elston
am 18 Mai 2020
I'd like to produce lists that represent the different permutations of swapping the columns of a matrix.
For example if I had and I wanted to produce two lists:
1) [ab, cd]
2) [ba,dc]
The matrix I am actually dealing with is 200x10
I have tried using
for i = 1 : 4
d = a_x(:, i)
a_x(:, i) = a_x(:, i + 1)
a_x(:,i + 1) = d
end
but it is not very efficient.
4 Kommentare
James Tursa
am 17 Mai 2020
What are you doing with these matrices downstream in your code? Maybe you can accomplish your desired result, or a statistical representation of your final result, without physically generating all of the matrices.
Akzeptierte Antwort
David Goodmanson
am 18 Mai 2020
Hi Robert,
Going with permutaions of the first six columns in conjuction with permutations of the last four columns certainly reduces the size of the problem by a huge factor, down to 'only' 17,280 matrices (or lists, if you prefer that name). But still, if you were to save those matrices you would be saving a lot of redundant information. The only thing that matters is the order of the columns, not really the contents of each one because you have the original matrix available. So with 200 rows you are saving 200 times as much information as you need.
In addition, how would you name the matrices so that you can access them? Dynamically named objects, (i.e. a separate name for each matrix) are definitely not a good idea because the code is susceptible to error and difficult to modify.
For matrix A with 10 columns, and a given permutation, say p = [3 6 2 1 5 4 10 7 9 8], what comes to mind for a name is A36215410798. But all you are doing is freezing in stone a process that is easily done on the spot. For a given permutation you can just do
B = A(:,p)
when needed.
Going back to the case of N columns and N! permutations, it makes much more sense to map each permutation to an index that runs from 1 to N! Then an index can produce a permutation which can produce a matrix as above. Here is some code that produces permutations from indices in bijective (one-to-one) fashion.
function j = ind2perm(N,n);
% index n mapped onto permutation j of N objects, with 1 <= n <= N!
% inverse function is perm2ind
% David Goodmanson
%
% j = ind2perm(N,n);
n = n-1; % zero based calculation
j = zeros(1,N);
int = 1:N;
for k = N:-1:1
a = rem(n,k)+1; % euclidean remainder +1
n = floor(n/k);
j(N-k+1) = int(a);
int(a) = [];
end
function n = perm2ind(j);
% permutation j of N objects mapped onto index n, with 1 <= n <= N!
% inverse function is ind2perm
% David Goodmanson
%
% n = perm2ind(j);
N = length(j);
c = cumprod(N+1:-1:2)/(N+1);
int = 1:N;
n = 0;
for k = 1:N-1;
f = find(int==j(k)); % euclidean remainder +1
int(f) = [];
n = n + (f-1)*c(k);
end
n = n+1; % go to one-based index
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!