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..

 채택된 답변

Roger Stafford
Roger Stafford 2013년 5월 11일

0 개 추천

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.)

추가 답변 (2개)

Image Analyst
Image Analyst 2013년 5월 11일

0 개 추천

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?
Ayse Kazan
Ayse Kazan 2013년 5월 12일
편집: Image Analyst 2013년 5월 12일

0 개 추천

Thank you for the answers. Yes I mean the shuffle. And yes the matrix entries (cells) are non-negative integers. An example;
A = [15 20;
10 30;
40 30;
50 10;
15 35]
row sums: 35,40,70,60,50
column sums: 130, 125.
shuffle;
B= [10 25;
20 20;
55 15;
40 20;
5 45]
row sums: 35,40,70,60,50 (fixed)
column sums: 130, 125 (fixed)

댓글 수: 5

Image Analyst
Image Analyst 2013년 5월 12일
Why do you need this? What is the "use case"? Did you try Roger's code?
Ayse Kazan
Ayse Kazan 2013년 5월 12일
I need to make shuffles (say N times, like making Bootstrap or Permutation test), then I will make calculations, association analyses like ki-square. This is for my presentation before beginning to write my thesis. This will be like a little example in my presentation.
I met with an error in Roger's code which is;
??? Error using ==> randperm Too many input arguments.
dont know what that is…
Here's Roger's code:
A = [15 20;
10 30;
40 30;
50 10;
15 35]
sumsAlongRows = sum(A, 2)
sumsAlongColumns = sum(A, 1)
[n, m] = size(A)
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
A
sumsAlongRows = sum(A, 2)
sumsAlongColumns = sum(A, 1)
It works fine for me but if you have an old version of MATLAB, you'll have to replace
r = randperm(n,2);
with
r = randperm(n);
r = r(1:2);
and do the same for c.
Ayse Kazan
Ayse Kazan 2013년 5월 12일
oh super! thank you so much.
yes mine is an old version so I did what so said and it worked!
thanks again. to both Roger and you...
Payal Verma
Payal Verma 2017년 3월 20일
how can i apply it on a image.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2013년 5월 11일

댓글:

2017년 3월 20일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by