Since matlab is slow when executing for loop, I usually avoid for loop for all my codes, and turn those into matrix calculation, which would be way fast. But here is a problem I can not find a smart way:
I have a n x n matrix
A=[a1,a2,a3,...,an],
here a1,a2,a3....an are columns of the matrix.
Another n x n matrix
B=[b1,b2,b3,...,bn],
similarly b1,b2,b3... are also columns of B.
And also a n x n matrix M.
I want to calculate the n x n matrix
C=[c1,c2,c3,...,cn],
thus (M+diag(ai))*ci = bi.
namely
ci = (M+diag(ai))\bi.
I know one way without for loop is:
C(:)=( blkdiag(M)+diag(A(:)) )\B(:).
But this will do too much calculation than needed.
Any smart solutions? you can assume there is no singularity problem in the calculation.