How to compute cheaply this matrix product in MATLAB

65 Views Asked by At

is there a way to compute this matrix product more efficently then the standard MATLAB multiplication?

$$D_1A^TD_2A$$ where $D_1$ and $D_2$ are $n\times n$ diagonal matrices.

I tried this script but maybe it is not so cheap.

function B=prodiag(d1,d2,A) N=size(A,1); for(i=1:N) c_ij=zeros(1,N); for(j=1:N) c_ij=c_ij+d2(j)*A(j,i)*A(j,:); end B(i,:)=d1(i)*c_ij; end end

1

There are 1 best solutions below

0
On BEST ANSWER

If you store d1 as a column vector and d2 as a row vector, you could do

d1(:,ones(n,1)).*A'.*d2(ones(1,n),:)*A;