Title: compute only diagonal of product of matrices in Matlab
Tags: matlab;matrix;matrix-multiplication
Question: Is there an efficient way in Matlab to compute only the diagonal of a product of 3 (or more) matrices?  Specifically I want

```diag(A'*B*A)
```

When A and B are both very large it can take a long time.  If there are only two matrices

```diag(B*A)
```

then I can quickly do it this way:

```sum(B.*A',2)
```

So right now I calculate the diagonal in the case with 3 matrices like this:

```C = B*A;
ans = sum(A'.*C',2);
```

which helps a lot, but the first operation (C = B*A) still takes a long time.  The whole thing must be repeated a number of times as well, resulting in weeks to run my code.  For example, B is about 15k x 15k and A is about 32k x 15k.  And nothing is sparse.

Comment: Yes, I do mean A is 15k x 32k.  Thanks for finding that mistake.

Comment: You mean A is about 15k x 32k, right? ;-)

Comment: It looks like you can't avoid doing the first of the full multiplications (`C` in your code), as all of the elements in `C` are needed in order to do the second multiplication

Comment: With matrices that large and not sparse.... I think you're SOL.

Here is the accepted answer: First of all, welcome! To be honest, this seems to be difficult. A little change is at least slightly increasing the speed:

```N = 5000;
A = rand(N,N*2);
B = rand(N,N);

t = cputime;
diag(A'*B*A);
disp(['Elapsed cputime ' num2str(cputime-t)]);

t=cputime;
C = B*A;
sum(A'.*C',2);
disp(['Elapsed cputime ' num2str(cputime-t)]);

% slightly better...
t=cputime;
C = B*A;
sum(A.*C)';
disp(['Elapsed cputime ' num2str(cputime-t)]);

% slightly better than slightly better...
t=cputime;
sum(A.*(B*A))';
disp(['Elapsed cputime ' num2str(cputime-t)]);
```

Results:

```Elapsed cputime 82.2593
Elapsed cputime 28.6106
Elapsed cputime 25.8338
Elapsed cputime 25.7714
```



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:05/28/14', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<matlab><matrix><matrix-multiplication>', 'Title': 'compute only diagonal of product of matrices in Matlab', 'ViewCount': 1466, 'id': 23903021, 'user_AboutMe': None, 'user_AccountId': 4531967.0, 'user_DisplayName': 'user3682146', 'user_Reputation': 43.0, 'user_Views': 1.0, 'user_id': 3682146.0}}