Making permutations in a matrix
古いコメントを表示
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..
採用された回答
その他の回答 (2 件)
Image Analyst
2013 年 5 月 11 日
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
2013 年 5 月 12 日
編集済み: Image Analyst
2013 年 5 月 12 日
5 件のコメント
Image Analyst
2013 年 5 月 12 日
Why do you need this? What is the "use case"? Did you try Roger's code?
Ayse Kazan
2013 年 5 月 12 日
Image Analyst
2013 年 5 月 12 日
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
2013 年 5 月 12 日
Payal Verma
2017 年 3 月 20 日
how can i apply it on a image.
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!