How does one sum 16, 3x3 matrices as efficiently as possible?
Afficher commentaires plus anciens
I need to sum 16, 3x3 matrices as efficiently as possible. There are 4 of each "Qbar" matrix to be summed e.g., 4x "Qbar_0", 4x "Qbar_45", 4x "Qbar_90" and 4x "Qbar_neg45". How do i do this. I'm very new to MatLab so a detailed explination with an example would be very helpful.
Qbar_0=1e11*[1.6297 0.1169 0; 0.1169 0.3913 0; 0 0 0.0734]
Qbar_45=1e10*[6.3712 4.9030 3.0955; 4.9030 6.3712 3.0959; 3.0959 3.0959 4.4677]
Qbar_90=1e11*[0.3913 0.1169 0; 0.1169 1.6297 0; 0 0 0.00734]
Qbar_neg45=1e10*[6.3712 4.9030 -3.0959; 4.9030 6.3712 -3.0959; -3.0959 -3.0959 4.4677]
Qbar_i=sum(4*Qbar_0, 4*Qbar_45, 4*Qbar_90, 4*Qbar_neg45)??
2 commentaires
Torsten
le 12 Déc 2022
Why not
Qbar_i=4*Qbar_0+4*Qbar_45+4*Qbar_90+4*Qbar_neg45
?
Robert Ody
le 12 Déc 2022
Réponses (2)
John D'Errico
le 12 Déc 2022
First, DON'T store all of those matrices as different arrays. Store tham as one 3-dimensional array, as a 3x3x16 array.
Then the sum is trivial.
Asum = sum(A,3);
2 commentaires
Robert Ody
le 12 Déc 2022
format long
Q(:,:,1:4) = repmat(1e11*[1.6297 0.1169 0; 0.1169 0.3913 0; 0 0 0.0734],1,1,4);
Q(:,:,5:8) = repmat(1e10*[6.3712 4.9030 3.0955; 4.9030 6.3712 3.0959; 3.0959 3.0959 4.4677],1,1,4);
Q(:,:,9:12) = repmat(1e11*[0.3913 0.1169 0; 0.1169 1.6297 0; 0 0 0.00734],1,1,4);
Q(:,:,13:16) = repmat(1e10*[6.3712 4.9030 -3.0959; 4.9030 6.3712 -3.0959; -3.0959 -3.0959 4.4677],1,1,4);
Qbar_i=sum(Q,3)
Qbar_0=1e11*[1.6297 0.1169 0; 0.1169 0.3913 0; 0 0 0.0734];
Qbar_45=1e10*[6.3712 4.9030 3.0955; 4.9030 6.3712 3.0959; 3.0959 3.0959 4.4677];
Qbar_90=1e11*[0.3913 0.1169 0; 0.1169 1.6297 0; 0 0 0.00734];
Qbar_neg45=1e10*[6.3712 4.9030 -3.0959; 4.9030 6.3712 -3.0959; -3.0959 -3.0959 4.4677];
Qbar_i=4*Qbar_0+4*Qbar_45+4*Qbar_90+4*Qbar_neg45
Walter Roberson
le 13 Déc 2022
0 votes
Suppose that you happened to know that on the intel i9-12900K with two banks of DDR5 memory running at 4664 MHz, that the internal timing logic finished every 13th double precision addition just slightly earlier than might otherwise be expected, and so if you had just the right instructions already sitting in cache, that you could complete 27 additions in 104 cycles whereas normally you would expect it to take 106 cycles for that. In such a situation, to perform the additions as efficiently as possible you would want to tune your code extensively to get everything into just the right place at the right time to gain 1.9% efficiency. It would probably only take you a month or so of extensive coding and testing in order to be save 10 cycles over the course of the additions you want to do.
Now suppose that you happened to know that on the i9-12900KF that the same timing oddity did not apply and that you would lose 2 cycles going through the same process. Suppose further that testing which CPU model you are using was going to take 6 cycles. If you could be sure that your code was being executed on i9-12900K then you could, with only a month or so of effort, save those 10 cycles, but otherwise you are going to lose cycles. If you knew you were going to do a lot of additions then you could have logic that tested the CPU model once and linked in the appropriate addition routine (save 10 on particular model, don't bother with the trouble on the other models because it would be slower.)
You really have to ask yourself whether it is worth the effort.
Are there some cases where it is important to have reactions literally as fast as possible on the system? Yes -- for example on a system monitoring nuclear reactors it might be important to engage the safety mechanisms as fast as you possibly can. But otherwise... you should probably just let MATLAB do the work in most cases; possibly drop into a MEX routine if the additions are unusually important.
Catégories
En savoir plus sur Geometry and Mesh dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!