how to preferentially compute vector products rather than matrix products
Show older comments
When performing a multiplication operation between a matrix and a vector, it seems that MATLAB's default settings are to be executed from the left.
This method has a problem in that calculation time is too long when complex multi-term matrix operations are performed.
Is there any way to change this setting without bracket?
For instance, how to preferentially compute vector products rather than matrix products.
Please, let me know.
N = 5000;
A = rand(N);
B = rand(N);
C = rand(N);
D = rand(N,1);
tic
E1 = A*B*C*D;
toc
tic
E2 = A*(B*(C*D));
toc
Accepted Answer
More Answers (1)
Paul
on 1 Aug 2023
0 votes
"Is there any way to change this setting without bracket?"
No. From the doc page for Operator Precedence: "Within each precedence level, operators have equal precedence and are evaluated from left to right."
However, that statement should say "... most are evaluated ..." as that page show exceptions to the general rule. But mtimes, * is not one of those exceptions.
1 Comment
Lee Jong Hyun
on 1 Aug 2023
Edited: Lee Jong Hyun
on 1 Aug 2023
Categories
Find more on Mathematics and Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!