Reputation: 685
I have a list of vectors given by a matrix where each line is a vector x_i. Now i want to make the following matrix
M = sum_i c_i*(x_i*x_i' - E(x_i*x_i'))
where c_i is a number and E(x_i*x_i') = sum_i x_i*x_i'/n , n is the number of lines.
One poor code for this is
x = rand(50,10000);
c = rand(10000,1);
M = zeros(50,50);
Y = zeros(50,50);
for i=1:size(x,2)
M = M + x(:,i)*x(:,i)';
end;
M = M/size(x,2);
for i=1:size(x,2)
Y = Y + c(i)*(x(:,i)*x(:,i)' - M);
end;
Upvotes: 1
Views: 1069
Reputation: 1739
Your question is incomplete, but here's something that matches your code:
M = x*x' / size(x, 2); % first for loop
Y = x*diag(c)*x' - sum(c) * M;
Note that your c
is probably wrong. Use c = rand(10000,1);
to get a vector.
Upvotes: 1
Reputation: 20915
Since the multiplication by transpose is nothing but a sum of multiplications, the answer is:
s = sum(x.*x,2)
Upvotes: 0