I agree with John's assessment. Unless dC, b, and C are terms of art for chemical diffusion problems I'd use more descriptive variable names. If you show this code to someone else, or if you need to revisit it six months from now, will it be easy to understand what it's doing?
But reordering the terms can help slightly:
C(:,:,c)=(dC*C(:,:,c)+(dC*C(:,:,c)')'+C(:,:,c)*(-b)+C(:,:,c));
Remove an extraneous pair of parentheses.
C(:,:,c)=dC*C(:,:,c)+(dC*C(:,:,c)')'+C(:,:,c)*(-b)+C(:,:,c);
Reorder terms.
C(:,:,c)=dC*C(:,:,c)+C(:,:,c)*(-b)+C(:,:,c)+(dC*C(:,:,c)')';
Assuming b is a scalar, combine the first two terms. If it's not there's still a way to simplify this, using the result of a later step.
C(:,:,c)=(dC-b)*C(:,:,c)+C(:,:,c)+(dC*C(:,:,c)')';
Combine the new first two terms.
C(:,:,c)=(dC-b+1)*C(:,:,c) + (dC*C(:,:,c)')';
Use the fact that (A*B)' = B'*A'.
C(:,:,c)=(dC-b+1)*C(:,:,c) + C(:,:,c)*dC';
If b is not a scalar, -b would be combined with the second term here not the first. This is the later step I referenced above.
Assuming dC and b don't change inside the loop over the pages of C you can precompute (dC-b+1) and dC' before the loop. This avoids needing to transpose slices of C then transpose a second time.
And one nitpick: unless C is of size 1 in the third dimension, it's not a matrix as ismatrix will return false.
4 Comments
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/486996-can-this-line-in-a-code-be-written-in-a-smarter-way#comment_759434
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/486996-can-this-line-in-a-code-be-written-in-a-smarter-way#comment_759434
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/486996-can-this-line-in-a-code-be-written-in-a-smarter-way#comment_759437
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/486996-can-this-line-in-a-code-be-written-in-a-smarter-way#comment_759437
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/486996-can-this-line-in-a-code-be-written-in-a-smarter-way#comment_759438
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/486996-can-this-line-in-a-code-be-written-in-a-smarter-way#comment_759438
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/486996-can-this-line-in-a-code-be-written-in-a-smarter-way#comment_759460
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/486996-can-this-line-in-a-code-be-written-in-a-smarter-way#comment_759460
Sign in to comment.