Diagonal Elements of the Square Matrix

조회 수: 2 (최근 30일)
MByk
MByk 2017년 10월 7일
편집: MByk 2017년 10월 7일
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))));

채택된 답변

John D'Errico
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
MByk
MByk 2017년 10월 7일
편집: MByk 2017년 10월 7일
Thank you for your reply. It is working but I still didn't understand why I get zeros instead of ones.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

James Tursa
James Tursa 2017년 10월 7일
Another way:
M(1:size(M,1)+1:end) = 1;

카테고리

Help CenterFile Exchange에서 Dimensionality Reduction and Feature Extraction에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by