Effacer les filtres
Effacer les filtres

Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

how to calculate similatity

2 vues (au cours des 30 derniers jours)
kmla
kmla le 1 Mar 2018
Clôturé : MATLAB Answer Bot le 20 Août 2021
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 commentaires
Jos (10584)
Jos (10584) le 1 Mar 2018
Modifié(e) : Jos (10584) le 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)) ...
kmla
kmla le 1 Mar 2018
i modified it

Réponses (2)

Walter Roberson
Walter Roberson le 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 commentaires
kmla
kmla le 1 Mar 2018
what is n
Walter Roberson
Walter Roberson le 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
elham kreem le 6 Mar 2018
Modifié(e) : Walter Roberson le 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 commentaires
Walter Roberson
Walter Roberson le 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>
elham kreem
elham kreem le 7 Mar 2018
you are a gentleman and intelligent

Cette question est clôturée.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by