reshuffle a symmetric matrix

1 次查看(过去 30 天)
Hi all, I have a large symmetric matrix (600*600)and need to reshuffle the elements randomly while keeping the matrix symmetric.
Could anyone help? I shall be grateful.
Regards, Shafique

采纳的回答

Andrei Bobrov
Andrei Bobrov 2011-6-13
1.
sz = size(A);
B = zeros(sz);
vtl = nonzeros(tril(reshape(1:numel(A),sz)));
B(tril(true(sz))) = A(vtl( randperm(length(vtl))));
B = B + tril(B,-1).';
2. More variant - don't modify main diagonal
sz = size(A);
B = diag(diag(A));
vtl = nonzeros(tril(reshape(1:numel(A),sz),-1));
B(tril(true(sz),-1)) = A(vtl( randperm(length(vtl))));
B = B + tril(B,-1).';
3. OR main diagonal - zeros (modify last row of the previous cod)
B = B + tril(B,-1).'
  4 个评论
Muhammad Shafique
Muhammad Shafique 2011-6-13
Hi Andrei. I needed to reshuffle the elements only within a triangle but excluding the diagonal. The variant you added does exactly that. Thanks a lot.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by