I have a matrice named vectors[i][j].I would like to calculate cosine similarity between each row. For example for this matrice
1 0 1 0 1 0 0
v= 0 0 1 1 1 0 1
1 1 0 0 1 0 1
I want to have similarity calculation ,between row1 and row 2 , row1 and row3, row2 and row3.Further more if similarity between row1 and row2 equal = 0.6 and others 0.5 and 0.4 respectively. I would like to add this value on every element(e=!0) of these to rows and get final matrice like this.
2.1 0 2.1 0 2.1 0 0
v= 0 0 2 2 2 0 2
1.9 1.9 0 0 1.9 0 1.9
Here is the part of code where i defined and filled my matrice;
string text = Request.Form["TextBox1"]; ; // text
string[] textInArray = text.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
int[,] vectors = new int[textInArray.Length, keywords.Length];
for (int i = 0; i < textInArray.Length; i++)
{
string[] words = textInArray[i].Split(' ');
for (int j = 0; j < keywords.Length; j++)
{
foreach (var word in words)
{
if (word.Contains(keywords[j]))
{
vectors[i, j]++;
}
}
}
}
and Here is my code to calculate similarity but i think it is not complete somewhere I have mistakes and I have no idea how can i add this value on elements of current two rows.
for(i=1 i<matrix.GetLength(0) i++){
for(j=1 j<matrix.GetLength(0) j++){
dot += vectors[i] * vectors[j];
mag1 += Math.Pow(vectors[i], 2);
mag2 += Math.Pow(vectors[j], 2);
}
float M= dot / (Math.Sqrt(mag1) * Math.Sqrt(mag2));
}
}