reshuffle a symmetric matrix

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

 Respuesta aceptada

Andrei Bobrov
Andrei Bobrov el 13 de Jun. de 2011

3 votos

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 comentarios

Muhammad Shafique
Muhammad Shafique el 13 de Jun. de 2011
Dear Andrei,
Many thanks for taking the trouble to help. Your solution works great but it would be perfect if the values of main diagonal could be kept from getting into triangles.
Your help is immensely appreciated.
Regards,
Shafique
Andrei Bobrov
Andrei Bobrov el 13 de Jun. de 2011
Hi! Muhhammad! Do you want to modify the elements of the matrix without affecting the main diagonal?
Muhammad Shafique
Muhammad Shafique el 13 de Jun. de 2011
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.
Andrei Bobrov
Andrei Bobrov el 13 de Jun. de 2011
Muhhammad! Point 2.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Computational Geometry en Centro de ayuda y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by