Fast computation of diagonal elements

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.

 Akzeptierte Antwort

Matt J
Matt J am 5 Nov. 2013
Bearbeitet: Matt J am 5 Nov. 2013

1 Stimme

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

2 Kommentare

or
sum((A'*B).*A',2)
Matt J
Matt J am 5 Nov. 2013
It works. But you'll have overhead from the transpositions.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Sean de Wolski
Sean de Wolski am 5 Nov. 2013

0 Stimmen

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 Kommentare

Saurabh
Saurabh am 5 Nov. 2013
I need to compute this for possibly 200 iterations.
Sean de Wolski
Sean de Wolski am 5 Nov. 2013
Bearbeitet: Sean de Wolski am 5 Nov. 2013
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'

Melden Sie sich an, um zu kommentieren.

Azzi Abdelmalek
Azzi Abdelmalek am 5 Nov. 2013
Bearbeitet: Azzi Abdelmalek am 5 Nov. 2013

0 Stimmen

This will reduce the time
S=A'*B;
d1=arrayfun(@(x) S(x,:)*A(:,x),1:n);

Kategorien

Gefragt:

am 5 Nov. 2013

Kommentiert:

am 5 Nov. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by