How to construct this matrix without using two for loops?

2 views (last 30 days)
Mohit Kumar
Mohit Kumar on 13 May 2021
Commented: VBBV on 13 May 2021
Hi, I'm trying to construct a matrix in the following manner:
sz=10;
V=rand(sz,sz);
X=rand(sz,sz);
for iter1=1:sz
for iter2=1:sz
A(iter1,iter2) = sum((V(:,iter1)*V(:,iter2)').*X,[1 2]);
end
end
Is there a way to do this without for-loops? I'm sure there must be a way that I haven't been able to think of!
  2 Comments
Mohit Kumar
Mohit Kumar on 13 May 2021
My bad, i mean iter1 and iter2. Edited the question. Thanks.

Sign in to comment.

Answers (2)

Mohit Kumar
Mohit Kumar on 13 May 2021
I was able to figure out the answer to this. The formulation can be written as
A = V' * X * V;

VBBV
VBBV on 13 May 2021
%if true
A = V'.* X.*V;
  1 Comment
VBBV
VBBV on 13 May 2021
Whichever order you chose to multiply it works as there is no conflict in dimensions or sizes of matrices

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!