Info
This question is closed. Reopen it to edit or answer.
how to calculate similatity
1 view (last 30 days)
Show older comments
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 Comments
Jos (10584)
on 1 Mar 2018
Edited: Jos (10584)
on 1 Mar 2018
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)) ...
Answers (2)
Walter Roberson
on 1 Mar 2018
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 Comments
Walter Roberson
on 1 Mar 2018
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
on 6 Mar 2018
Edited: Walter Roberson
on 6 Mar 2018
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 Comments
Walter Roberson
on 6 Mar 2018
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>
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!