Multiplication on the diagonal of a submatrix
Show older comments
I have a matrix A and two index vectors u and v of the same size. I want to compute the product of the entries on the diagonal of A(u,v). I tried prod(diag(A(u,v))). It is faster than the for loop. But A(u,v) also includes the redundant off-diagonal entries. So, I wonder if there is a more efficient way to do it.
n = 10^4;
d = 50;
A = rand(n);
u = randi(n,d,1);
v = randi(n,d,1);
tic
prod_1 = 1;
for k = 1:50
prod_1 = prod_1*A(u(k),v(k));
end
toc
tic
prod_2 = prod(diag(A(u,v)));
toc
Accepted Answer
More Answers (0)
Categories
Find more on Operating on Diagonal Matrices 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!