Writing a summation code where i≠j
Ältere Kommentare anzeigen
.

Here, x is a 1xn vector, and w is a square matrix where w_ii=0
I wrote the code to calculate the denominator, but it's quite complicated to calculate the nominator.
I tried with the below code, but I'm not sure it's right...
How can I do that?
x = [1,2,3,4]; % feature vector
w= rand(4,2); D=pdist(w);
W= squareform(D); % weight matrix
% Calculate Denominator
denom=0;
for i = 1:4
for j = 1:4
if i~=j
denom = denom + x(j)*x(i);
end
end
end
% Calculate nominator
Wu = triu(W,1);
Wd = tril(W,-1);
nom = (sum(sum(Wu)) + sum(sum(Wd))) * denom;
Akzeptierte Antwort
Weitere Antworten (2)
Abhishek Chakram
am 10 Jun. 2022
Hi,
You can do it the same way as you calculated the denominator. The code will look like :
numerator = 0;
for i = 1:4
for j = 1:4
if i~=j
numerator = numerator + W(i,j)*x(i)*x(j);
end
end
end
1 Kommentar
aposta95
am 10 Jun. 2022
David Hill
am 10 Jun. 2022
No loops needed
G=sum(w.*x.*x','all')/sum(x.*x','all');
1 Kommentar
Jan
am 10 Jun. 2022
Isn't the "for all i~=j" missing?
Kategorien
Mehr zu Linear Algebra finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!