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일

1 개 추천

diagonal = sum(A.*(B*A),1);

댓글 수: 2

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일

0 개 추천

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일

0 개 추천

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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

질문:

2013년 11월 5일

댓글:

2013년 11월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by