how to write a code for cosine similarity mentioned in images?

13 views (last 30 days)
Fariba Dehghan
Fariba Dehghan on 7 Jan 2022
Answered: yanqi liu on 8 Jan 2022
I want to write the code for function that I mentioned in the images, where M = 96. The output power for the ith day is pi =[p , . . . , piM ]T , ∀i ∈ [1, . . . , 366]. j ∈ [1, . . .,366] and pjm is the mth output power point in the jth day, pj = [p , . . . , pjM ]T. k is an interval value between the ith day and the jth day, i + k = j. I wrote following code but it does not work correctly.
can anyone help me on this?
thanks for your consideration.
CS=zeros(96,96); % Initialise output
syms k
h=1/(366-k);
for m=1:96
for i=1:366
j=i+k;
if j<=366
Pi(:,i)=mat_solar_(:,i);
Pj(:,i)=mat_solar_(:,i:j);
end
end
end

Answers (1)

yanqi liu
yanqi liu on 8 Jan 2022
clc; clear all; close all;
x = 1:9;
y = magic(3);
x = x(:)';
y = y(:)';
r = 1-pdist2(x, y, 'cosine')
r = 0.7895
% self define
a = sum(x.*y);
b = norm(x)*norm(y);
c = a/b
c = 0.7895

Community Treasure Hunt

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

Start Hunting!

Translated by