Making permutations in a matrix
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ayse Kazan
il 11 Mag 2013
Commentato: Payal Verma
il 20 Mar 2017
Hi,
I have an nxm matrix. I need to make permutations but the row and coloumn sums must be fixed (only the matrix elements will change).
How can I do that?
Thanks..
0 Commenti
Risposta accettata
Roger Stafford
il 11 Mag 2013
I can only guess at your meaning here of the word 'permutations'. Based on your other similar request at
http://www.mathworks.com/matlabcentral/answers/75431,
I assume you mean that all matrix entries are to be non-negative integers and that, correspondingly, the permutations are to be considered as among objects for which these are the counts - that is, an arrangement of [5,7,11,4] like objects is considered a "permutation" or rearrangement of [4,7,3,13] objects. (This is not what I would call a true permutation, however.)
With N set to a very large number, the following can approach such a random adjustment in which the row and column sums are preserved with all elements remaining non-negative. Let A be your n x m matrix.
N = 10000;
for k = 1:N
r = randperm(n,2); r1 = r(1); r2 = r(2);
c = randperm(m,2); c1 = c(1); c2 = c(2);
d = randi([-min(A(r1,c1),A(r2,c2)),min(A(r1,c2),A(r2,c1))]);
A([r1,r2],[c1,c2]) = A([r1,r2],[c1,c2]) + [d,-d;-d,d];
end
At each step this adjusts four elements lying in two columns and two rows so as to preserve row and column sums and such that those elements remain non-negative integers.
(This is a bit crude and cumbersome but it is all I could think of at the moment.)
0 Commenti
Più risposte (2)
Image Analyst
il 11 Mag 2013
What is your definition of permute? You mean like shuffle/scramble the columns (each column goes to a different place)? I don't think you can do that, in general, without changing the sums. Look at this matrix
1 2
1 1
There's no way you can permute anything with that without changing either the sum of each column, or the sum of each row. Something is going to change.
Do you have one small example that you can show to demonstrate what you are thinking of?
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrix Indexing in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!