我有一个对称的邻接矩阵,其对角线上的值为零。现在我正在寻找重新排序方法来显示社区,它将两个派系中的矩阵分别用+1和-1值。 如果有人能在这方面帮助我,我们将不胜感激。
例如:matrix(10,10)
0 1 -1 1 1 -1 1 1 -1 -1
1 0 -1 1 1 -1 1 -1 -1 -1
-1 -1 0 -1 -1 1 1 1 1 -1
1 1 -1 0 1 -1 1 -1 -1 -1
1 1 -1 1 0 -1 1 1 -1 -1
-1 -1 1 -1 -1 0 -1 1 1 1
1 1 1 1 1 -1 0 1 1 1
1 -1 1 -1 1 1 1 0 -1 -1
-1 -1 1 -1 -1 1 1 -1 0 1
-1 -1 -1 -1 -1 1 1 -1 1 0
输出必须是:
1 1 1 1 1 -1 -1 -1 -1 -1
1 1 1 1 1 -1 -1 -1 -1 -1
1 1 1 1 1 1 -1 -1 -1 -1
1 1 1 1 1 -1 -1 -1 -1 -1
1 1 1 1 1 -1 -1 -1 -1 -1
-1 -1 -1 -1 -1 1 1 1 1 1
-1 -1 -1 -1 -1 1 1 1 1 1
-1 -1 -1 -1 -1 1 1 1 1 1
-1 -1 -1 -1 -1 1 1 1 1 1
-1 -1 -1 -1 -1 1 1 1 1 1
零条目可视为1
答案 0 :(得分:0)
由于这个例子,我不确定如何处理大多数情况,所以我猜到了。
a = round(3*rand(10) - 1.5);
a(logical(eye(size(a)))) = 0;
b = cliques(a);
这保持了对称性要求,只需要在所有其他过滤器之后剩余一个({1}}的数量为-1
时,从-1
的“翅膀”中断。奇数然后保持对称一个元素必须在对角线上。)
function b = cliques(a)
a(a == 0) = 1;
n = sum(a(:) == -1);
s = floor(sqrt(n/2));
b = ones(size(a));
b(end - s + 1:end, 1:s) = -1;
b(1:s, end - s + 1:end) = -1;
n = n - 2*s^2;
bands = floor(n/4);
b(s+1, end - bands + 1:end) = -1;
b(end - bands + 1:end, s+1) = -1;
b(end - s, 1:bands) = -1;
b(1:bands, end - s) = -1;
n = n - 4*bands;
dots = floor(n/2);
if dots
b(end - s, s + 1) = -1;
b(s + 1, end - s) = -1;
end
n = n - 2*dots;
if n
b(1,1) = -1;
end
contourf(b, 'LevelList', [-1,1]);
set(gca,'Ydir','reverse')
sum(a(:)) == sum(b(:))
end