Skip to content

Commit adffaf6

Browse files
authored
Merge pull request #20 from atousa1/master
jaccard similarity added
2 parents f3a7226 + d94ca5e commit adffaf6

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
function p = linear_search(A,t)
2+
%% linear Search
3+
% This function linear searches target value (t) in array A.
4+
% For this, It sequentially checks each entry of the array until a match is found
5+
% or or the whole list has been searched.
6+
% If it can find the target returns 1 otherwise 0.
7+
8+
array_length = length(A);
9+
i = 1;
10+
searchTermination = 0;
11+
12+
while searchTermination == 0 && i < array_length+1
13+
if A(i) == t
14+
p = 1;
15+
searchTermination = 1;
16+
disp('the target is found in the array')
17+
else
18+
i = i+1;
19+
end
20+
end
21+
if i == array_length+1
22+
p = 0;
23+
disp('the target is not found in the array')
24+
end
25+
end
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function p = jaccard_similarity(A,B)
2+
%% jaccard similarity
3+
% This function calculates jaccard similarity index of inputs arrays A and
4+
% B. The formula to find the Index is (number of entries in both sets) / (number of entries in either set) * 100
5+
% The higher the percentage, the more similar the two arrays.
6+
% For this, each of input arrays is modified by removing its same entries
7+
% (except on them), then number of common entries between two new arrays is
8+
% calculated by comparing them.
9+
10+
modified_A = unique(A);
11+
modified_B = unique(B);
12+
13+
length_mA = length(modified_A);
14+
length_mB = length(modified_B);
15+
common_number = 0; %initialize the number of common entries
16+
17+
if length_mA <= length_mB
18+
X = modified_A;
19+
Y = modified_B;
20+
else
21+
X = modified_B;
22+
Y = modified_A;
23+
end
24+
25+
for i = 1:length(X)
26+
for j = 1:length(Y)
27+
if X(i) == Y(j)
28+
common_number = common_number + 1;
29+
end
30+
end
31+
end
32+
33+
total_number = length_mA + length_mB - common_number;
34+
p = (common_number/total_number)*100;
35+
end

0 commit comments

Comments
 (0)