Vectorising Multiplying each column from a matrix with a square matrix and the tranpose of this column
1 view (last 30 days)
Show older comments
Hello,
I have an operation that I'm doing with a loop but would like to vectorise if possible.
I have a matrix h (2790x3591) and a matrix Pn(2790x2790).
I need to implement the following operation: h_column' * Pn * h_column, for all columns of h.
Is there a way to vectorise this operation? It takes about 45s to do with the for loop.
NB: The matrices are complex with very small imaginary parts, so that adds a bit of delay because the multiplications are more involved.
0 Comments
Answers (3)
Kevin Holly
on 23 Feb 2022
Can you do the following?
h = rand(2790,3591);
Pn = rand(2790,2790);
size(h'*Pn*h)
James Tursa
on 23 Feb 2022
If you need the same column on each side of the multiply, then maybe this:
result = sum(h'*Pn.*h',2);
2 Comments
Matt J
on 23 Feb 2022
Edited: Matt J
on 23 Feb 2022
I need to take diag(h'*Pn*h).
No, that will be super slow and inefficient. This should be what you want,
result = sum(h.*(Pn*h),1);
James' version was almost equivalent except for a missing transpose,
result = sum(h'*Pn'.*h',2);
but no need to incur the expense of all those transpositions, as far as I can see.
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!