Diagonal Elements of the Square Matrix
2 次查看(过去 30 天)
显示 更早的评论
I am calculating Sorensen-Dice coefficient (2|A and B|/|A|+|B|) but diagonal elements (intersection with itself) of the square matrix is equal to zero. It is not a big problem but how can I convert them into ones. Thanks for the help.
v = [1 1 0 0 1 1 0; 1 1 0 1 0 0 0; 0 0 0 0 0 1 1]
d = squareform(pdist(v,@(a,b)(2 * sum((a.*b),2))./(sum(a) + sum(b,2))));
0 个评论
采纳的回答
John D'Errico
2017-10-7
If the diagonal is already EXACTLY zero, then this will suffice:
M = M + eye(size(M));
If they are only approximately zero, then you could do this:
M(find(eye(size(M)))) = 1;
For example,
M = magic(5)
M =
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
M(find(eye(size(M)))) = 1;
M
M =
1 24 1 8 15
23 1 7 14 16
4 6 1 20 22
10 12 19 1 3
11 18 25 2 1
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!