4 views (last 30 days)

Dear all:

I am trying to calculate a diagonal of a matrix (denoted A), which is formed by multiplying two large-dimensional matrices (denoted as B*C).

A naive way to do it is: first, calculating explicitly A = B*C, then get diagonal out from A. However, the first step takes forever to run due to the high-dimension of B and C. But the only thing I need is the diagonal of A.

Another straightforward way in my mind is: I could create a loop by calculating each element of the diagonal of A one by one. It will surely save a lot of time, but I am not sure if this is the most efficient way.

I am wondering if anyone knows a faster/smarter way to calculate it.

Thank you very much in advance!

Best,

Long

the cyclist
on 26 Mar 2020

Here is one way:

% Make up some inputs

N = 4;

B = rand(N);

C = rand(N);

% Calculate the diagonal

A_diag = 0;

for nr = 1:N

A_diag = A_diag + B(:,nr).*C(nr,:)';

end

Opportunities for recent engineering grads.

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

Start Hunting!
## 1 Comment

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/512998-get-the-diagonal-without-calculating-the-explicit-matrix#comment_815881

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/512998-get-the-diagonal-without-calculating-the-explicit-matrix#comment_815881

Sign in to comment.