Fast computation of diagonal elements
12 次查看(过去 30 天)
显示 更早的评论
I have two matrices, A of size m-by-n and B of size m-by-m. I need to quickly compute the digonal elements of (A'*B*A). Here m is of order 100 while n is of order 10000.
How would I do it ? Doing diag(A'*B*A) will be slow.
Neither B nor A is diagonal. they are full matrices.
0 个评论
更多回答(2 个)
Sean de Wolski
2013-11-5
m = 100;
n = 10000;
A = rand(m,n);
B = rand(m);
timeit(@()diag(A'*B*A)) % R2013b
ans = 0.8128
So it takes slightly less than a second on my Win7x64 laptop. How many times do you need to do this computation?
2 个评论
Sean de Wolski
2013-11-5
编辑:Sean de Wolski
2013-11-5
I.e. less than three minutes total? I would just do the above 200x and go get a cup of coffee while MATLAB is crunchin'
Azzi Abdelmalek
2013-11-5
编辑:Azzi Abdelmalek
2013-11-5
This will reduce the time
S=A'*B;
d1=arrayfun(@(x) S(x,:)*A(:,x),1:n);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!