Since matlab is slower to execute for a loop, I usually avoid a loop for all my codes and turn them into a matrix calculation, which would be fast. But here is a problem that I cannot find in a smart way:
I have an nxn matrix
A=[a1,a2,a3,...,an],
here a1, a2, a3 .... an are the columns of the matrix.
Another nxn matrix
B=[b1,b2,b3,...,bn],
similarly b1, b2, b3 ... are also columns B.
As well as nxn matrix M.
I want to calculate nxn matrix
C=[c1,c2,c3,...,cn], thus (M+diag(ai))*ci = bi.
namely
ci = (M+diag(ai))\bi.
I know one way without a loop:
C(:)=( blkdiag(M)+diag(A(:)) )\B(:).
But this will do too many calculations than necessary.
Any smart solutions? You can assume that there is no problem with the feature in the calculation.
matrix for-loop matlab
Min Lin 03 Dec '12 at 6:35 2012-12-03 06:35
source share