Search code examples
matlabsimilaritycosine-similarity

How to obtain complexity cosine similarity in Matlab?


I have implemented cosine similarity in Matlab like this. In fact, I have a two-dimensional 50-by-50 matrix. To obtain a cosine should I compare items in a line by line form.

for j = 1:50
    x = dat(j,:);
    for i = j+1:50
        y = dat(i,:);
        c = dot(x,y);
        sim = c/(norm(x,2)*norm(y,2));
    end
end

Is this correct? and The question is this: wath is the complexity or O(n) in this state?


Solution

  • Better end with 49. Maybe you should also add an index to sim?

    for j = 1:49
      x = dat(j,:);
      for i = j+1:50
          y = dat(i,:);
          c = dot(x,y);
          sim(j) = c/(norm(x,2)*norm(y,2));
      end
    end
    

    The complexity should be roughly like o(n^2), isn't it? Maybe you should have a look at correlation functions ... I don't get what you want to write exactly, but it looks like you want to do something similar. There are built-in correlation functions in Matlab.