How does one sum 16, 3x3 matrices as efficiently as possible?

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_0 = 3×3
1.0e+11 * 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_45 = 3×3
1.0e+10 * 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_90 = 3×3
1.0e+11 * 0.3913 0.1169 0 0.1169 1.6297 0 0 0 0.0073
Qbar_neg45=1e10*[6.3712 4.9030 -3.0959; 4.9030 6.3712 -3.0959; -3.0959 -3.0959 4.4677]
Qbar_neg45 = 3×3
1.0e+10 * 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)??
Invalid use of operator.
Error using sum
Dimension argument must be a positive integer scalar, a vector of unique positive integers, or 'all'.

2 commentaires

Why not
Qbar_i=4*Qbar_0+4*Qbar_45+4*Qbar_90+4*Qbar_neg45
?
For such a detailed explination, it worked a charm. Thank you!

Connectez-vous pour commenter.

Réponses (2)

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

How would I set it up as a 3x3x16 array?
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_i = 3×3
1.0e+12 * 1.318096000000000 0.485760000000000 -0.000016000000000 0.485760000000000 1.318096000000000 0 0 0 0.389712000000000
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
Qbar_i = 3×3
1.0e+12 * 1.318096000000000 0.485760000000000 -0.000016000000000 0.485760000000000 1.318096000000000 0 0 0 0.389712000000000

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 13 Déc 2022
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.

Produits

Version

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by