필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

how to calculate similatity

조회 수: 2 (최근 30일)
kmla
kmla 2018년 3월 1일
마감: MATLAB Answer Bot 2021년 8월 20일
i have 2 matrix i want to calculate the similarty
i tried this code but they give me false values(negative values)
x=[2 33;2 50;2 88;3 89;4 3;4 15;6 10;6 133;8 134;8 247;8 249;8 289;9 103;10 34;10 189;11 49];
X=[10 278;11 77;11 97;11 165;11 226;11 279;12 179;14 97;14 143;16 181;16 211;17 52;17 212;19 145];
m=length(x)
for i=1:m
s1=sum(x(i)-X(i));
s2=sqrt(sum((x(i)-X(i)).^2));
S(i)=s1/s2;
end
  댓글 수: 2
Jos (10584)
Jos (10584) 2018년 3월 1일
편집: Jos (10584) 2018년 3월 1일
How do your variables m1 and m2 relate to x and X?
Also note that m1(i) is only one value, equal to sum(m1(i))! You want to calculate m1(1)+m1(2)+m1(3)+..., so sum(m1(1:i)) ...
kmla
kmla 2018년 3월 1일
i modified it

답변 (2개)

Walter Roberson
Walter Roberson 2018년 3월 1일
You have arrays with 2 columns. Why are you indexing them with only a single index?
The formula you show appears to be valid only for two vectors that are the same size. You appear to have two 2D arrays that are of different size.
It is difficult to interpret that formula because it uses i as the variable of summation in both places, making it unclear which i in the second summation is the i from the first summation and which is from the second summation. This is important because the convention is that a summation continues over all terms in the same linear subexpression unless there are brackets that limit the summation the way that [] limit the second summation so that it is clear that the ^0.5 applies to the result of the second summation.
My guess is that the formula has not been written properly and that there should be [] around the first summation ending before the / . If I am correct then one way of writing the expression for the first column would be
m = min(size(m1,1), size(m2,1));
dot(m1(1:n,1),m2(1:n,1)) ./ sqrt(dot(m1(1:n,1).^2,m2(1:n,1).^2))
  댓글 수: 2
kmla
kmla 2018년 3월 1일
what is n
Walter Roberson
Walter Roberson 2018년 3월 1일
m = min(size(m1,1), size(m2,1));
dot(m1(1:m,1),m2(1:m,1)) ./ sqrt(dot(m1(1:m,1).^2,m2(1:m,1).^2))

elham kreem
elham kreem 2018년 3월 6일
편집: Walter Roberson 2018년 3월 6일
first : if you change name of variables as x ,y then two variables must the same linghth ,they are not the same try this code :
x=[2 33;2 50;2 88;3 89;4 3;4 15;6 10;6 133;8 134;8 247;8 249;8 289;9 103;10 34;10 189;11 49];
y=[10 278;11 77;11 97;11 165;11 226;11 279;12 179;14 97;14 143;16 181;16 211;17 52;17 212;19 145];
for i = 1 : 14
s = (sum(x(i)*y(i)) ) / (sum((x(i)^2)*(y(i)^2)))^(0.5)
end
with best
  댓글 수: 4
Walter Roberson
Walter Roberson 2018년 3월 6일
You have to modify that to account for the columns.
I compare some versions of the code:
m1=[2 33;2 50;2 88;3 89;4 3;4 15;6 10;6 133;8 134;8 247;8 249;8 289;9 103;10 34;10 189;11 49];
m2=[10 278;11 77;11 97;11 165;11 226;11 279;12 179;14 97;14 143;16 181;16 211;17 52;17 212;19 145];
m = min(size(m1,1), size(m2,1));
cols = size(m1,2);
Sdot = zeros(1,cols);
for C = 1 : cols
Sdot(C) = dot(m1(1:m,C),m2(1:m,C)) ./ sqrt(dot(m1(1:m,C).^2,m2(1:m,C).^2));
end
disp('dot (WDR)')
Sdot %#ok<NOPTS>
Slong = zeros(1,cols);
for C = 1 : cols
tnum = 0;
tden = 0;
for i = 1 : m
tnum = tnum + m1(i, C) .* m2(i, C);
tden = tden + m1(i, C).^2 .* m2(i, C).^2;
end
Slong(C) = tnum ./ sqrt(tden);
end
disp('long form')
Slong %#ok<NOPTS>
Sshort = zeros(1,cols);
for C = 1 : cols
Sshort(C) = sum(sum(m1(1:m,C)'*m2(1:m,C)))/ sum ((sum(m1(1:m,C).^2'*m2(1:m,C).^2)).^(0.5));
end
disp('Short form (Elham Kreem)')
Sshort %#ok<NOPTS>
Sshorter = zeros(1,cols);
for C = 1 : cols
Sshorter(C) = m1(1:m,C).'*m2(1:m,C) ./ sqrt(m1(1:m,C).^2.'*m2(1:m,C).^2);
end
disp('Shorter form (WDR)')
Sshorter %#ok<NOPTS>
Sloopless = diag( m1(1:m,:).'*m2(1:m,:) ./ sqrt(m1(1:m,:).^2.'*m2(1:m,:).^2)).';
disp('loopless (WDR)');
Sloopless %#ok<NOPTS>
elham kreem
elham kreem 2018년 3월 7일
you are a gentleman and intelligent

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by