Filter löschen
Filter löschen

Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

how to calculate similatity

2 Ansichten (letzte 30 Tage)
kmla
kmla am 1 Mär. 2018
Geschlossen: MATLAB Answer Bot am 20 Aug. 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 Kommentare
Jos (10584)
Jos (10584) am 1 Mär. 2018
Bearbeitet: Jos (10584) am 1 Mär. 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 am 1 Mär. 2018
i modified it

Antworten (2)

Walter Roberson
Walter Roberson am 1 Mär. 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 Kommentare
kmla
kmla am 1 Mär. 2018
what is n
Walter Roberson
Walter Roberson am 1 Mär. 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 am 6 Mär. 2018
Bearbeitet: Walter Roberson am 6 Mär. 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 Kommentare
Walter Roberson
Walter Roberson am 6 Mär. 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 am 7 Mär. 2018
you are a gentleman and intelligent

Diese Frage ist geschlossen.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by