Fast computation of diagonal elements

2 ビュー (過去 30 日間)
Saurabh
Saurabh 2013 年 11 月 5 日
コメント済み: Matt J 2013 年 11 月 5 日
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.

採用された回答

Matt J
Matt J 2013 年 11 月 5 日
編集済み: Matt J 2013 年 11 月 5 日
diagonal = sum(A.*(B*A),1);
  2 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 11 月 5 日
or
sum((A'*B).*A',2)
Matt J
Matt J 2013 年 11 月 5 日
It works. But you'll have overhead from the transpositions.

サインインしてコメントする。

その他の回答 (2 件)

Sean de Wolski
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 件のコメント
Saurabh
Saurabh 2013 年 11 月 5 日
I need to compute this for possibly 200 iterations.
Sean de Wolski
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
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);
  1 件のコメント
Saurabh
Saurabh 2013 年 11 月 5 日
This solution is slightly slower than Matt's.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by