sum of series of matrix

12 次查看(过去 30 天)
rakesh kumar
rakesh kumar2022-8-16
评论: Image Analyst ,2022-8-16
I have a series of matrices like A1, A2,.........A99. I want to find sum of three matrices like(A1+A2+A3) then (A4+A5+A6)........(A97+A98+A99)
  2 个评论
rakesh kumar
rakesh kumar 2022-8-16
yes, but my problem is that have to find the sum in groups as I have mentioned. Thanks in advance for your help.

请先登录,再进行评论。

采纳的回答

Chunru
Chunru 2022-8-16
A = randn(5, 5, 99);
B = zeros(5, 5, 33);
for i=1:33
B(:, :, i) = sum(A(:, :, (i-1)*3+(1:3)), 3);
end
whos
Name Size Bytes Class Attributes A 5x5x99 19800 double B 5x5x33 6600 double cmdout 1x33 66 char i 1x1 8 double
B
B =
B(:,:,1) = 1.0801 -0.9310 1.6476 3.9098 -0.1822 2.7773 1.0514 2.8541 -1.9000 0.6120 1.0582 -0.4614 0.8884 0.4824 0.5554 -0.5364 0.4871 0.6735 -0.6918 0.7854 -2.3907 1.8485 0.3522 -3.2762 -2.1711 B(:,:,2) = 1.7186 0.5920 3.6962 2.1688 0.1163 -0.7519 -1.6879 3.1578 -3.1428 1.4388 0.0607 2.9797 0.7485 0.6620 0.0260 1.8920 -3.0542 1.0073 1.4882 -0.6512 -4.2737 -0.4719 0.9764 0.6908 1.9635 B(:,:,3) = -1.1917 -0.1001 0.9368 -1.9018 0.7090 1.5334 1.7555 2.1941 -0.6463 -0.1665 -0.2419 0.3022 0.3150 -3.5558 1.1313 -1.6839 0.6759 0.0532 2.8834 4.0220 -0.1851 1.4545 2.3745 -1.1610 1.2608 B(:,:,4) = 0.6287 1.9192 -1.0544 -1.2425 -2.8365 1.4835 0.5123 4.1524 -1.1864 0.1390 0.7249 -0.1715 0.3567 -1.1869 2.5121 -1.0874 1.2256 -0.4822 1.7408 -0.8053 -0.7387 1.7543 1.1213 -4.0384 -0.8762 B(:,:,5) = 0.9368 0.3619 1.2844 -1.2814 -1.2918 -1.4238 0.3363 1.5450 0.0269 -1.8170 0.6068 0.5592 2.0891 -0.9862 -0.2391 -0.4027 -0.5556 -0.5730 1.7127 1.1396 1.8389 -1.2629 3.5192 0.3103 2.6123 B(:,:,6) = -3.2745 1.2307 -1.4399 3.2468 -2.8418 -0.9268 -2.3572 1.5603 1.1992 0.8358 0.3286 -0.3112 0.3418 0.1299 0.3077 0.1179 1.6223 -1.6759 3.0819 0.5428 3.4974 0.7914 1.4008 -0.8006 2.3222 B(:,:,7) = 3.8006 1.8363 2.5171 -0.0303 0.9158 2.4714 0.0860 -1.7693 -0.4328 0.2005 2.2799 -0.9735 -1.7687 0.4138 2.9406 -1.0887 1.7944 1.0271 0.7926 1.2619 -1.3412 -1.9069 2.7119 -3.4371 1.1389 B(:,:,8) = -1.7138 2.3615 2.5694 -0.4497 -4.5408 -1.5113 0.7602 2.1105 -0.3170 -2.6361 -3.1610 -1.4643 0.6216 2.8660 0.0573 -1.6175 4.0008 -0.3381 -0.1984 -0.7947 -1.7819 0.4095 -1.4420 1.5145 -0.3211 B(:,:,9) = 2.3838 2.0878 -0.5382 3.7507 2.0960 1.9347 2.0669 -0.8329 0.3824 0.8640 2.4861 -2.6501 1.7428 -1.7890 -1.6104 -0.7959 0.3277 4.8707 -0.2208 1.2169 -1.9939 -0.6632 -0.0665 -1.7913 1.4307 B(:,:,10) = 1.7708 0.8174 -2.7081 0.5897 -0.6022 -0.1805 0.3482 -1.1240 0.8698 -0.2955 -0.9489 1.3042 -4.6263 1.7611 0.8033 -1.0393 -1.3906 -2.6551 -0.4666 0.3346 2.1117 0.6587 0.1391 -2.2200 1.9483 B(:,:,11) = -1.6108 3.9748 2.3733 -1.2794 -1.1346 1.5313 2.6586 1.2973 1.1206 -0.6783 0.2285 1.3468 -0.3297 -1.4879 0.9589 -3.2108 3.4015 1.2998 -3.2940 0.4316 -1.2495 -3.5360 2.1075 -0.1034 -1.3260 B(:,:,12) = 0.0661 -1.7048 1.9452 3.1399 -3.0431 -0.6224 -1.3920 2.7349 1.5759 1.9048 -2.6062 0.1277 3.5493 0.0089 -6.0951 0.2745 -0.9913 -3.8000 -2.1058 0.0014 -0.6025 -0.3238 0.7430 1.2486 0.3905 B(:,:,13) = 0.5090 -1.8384 4.9040 -0.0834 0.9401 -2.5016 -1.0498 -0.9617 0.1988 -1.6728 3.1996 -2.7104 2.0117 1.0260 -0.3569 3.2603 3.1382 3.0901 0.0855 -0.6663 0.7472 1.9691 0.4188 0.2657 -0.7560 B(:,:,14) = -2.0834 -0.1674 -0.0127 -1.3724 -0.7853 0.0141 -1.2462 -2.0874 -1.8179 -0.6612 1.4417 -0.5028 0.7359 0.5513 -0.0937 0.5833 -2.1986 -1.0589 0.2680 1.2708 1.8445 -1.7178 -0.3624 2.5992 2.4769 B(:,:,15) = -1.9915 0.2211 -0.8792 1.7162 -1.1824 1.8791 0.1338 0.1207 0.7979 0.0575 2.6634 -0.9772 -1.4023 1.9158 -2.0530 -3.5620 0.7934 2.8845 3.0420 -0.4115 -2.0293 3.0953 0.7776 0.0454 -0.6836 B(:,:,16) = 0.2460 -0.8852 2.6075 0.4653 0.5208 0.9171 -2.8741 2.2960 -1.6905 -0.0163 0.1987 0.0810 1.4670 -0.0219 3.1898 -1.1416 0.1643 2.6641 3.5801 0.7277 -2.0222 -0.4112 -1.6089 -2.2642 -2.0376 B(:,:,17) = 0.7975 2.9341 0.3443 0.0866 0.5167 0.1475 -3.4558 1.9223 -2.5965 1.4561 -1.4942 -1.1344 0.9004 -0.6161 1.0252 -0.5623 1.3244 -2.1575 -0.8474 -0.6853 -1.4023 -0.5532 -0.4516 -0.5334 3.4513 B(:,:,18) = -2.9338 0.0491 1.8345 2.9347 -1.9308 -0.3876 3.3523 4.0483 0.2978 -0.5219 2.1096 0.3462 0.7005 -1.5934 2.8639 0.4757 -1.7640 -0.7932 2.9646 -0.7123 -1.2909 -1.8976 1.0907 1.8097 -0.5561 B(:,:,19) = 0.5579 0.2422 -2.9485 0.7972 2.5755 1.0189 0.1914 -0.7493 0.6939 -2.0909 -2.5138 2.6576 -1.2495 -0.5541 2.7864 -1.6242 0.5929 -0.1956 0.4622 -3.3564 -0.2555 -1.3294 2.4360 0.0018 0.2349 B(:,:,20) = -2.7642 -0.5471 0.3265 -1.7913 -2.0071 1.3997 0.1897 0.0740 1.5834 0.1635 0.2456 -3.2479 0.7067 -2.1452 0.9359 -1.1186 0.6781 1.9374 0.0016 -1.4852 0.1581 1.1187 -1.2812 1.5271 2.3767 B(:,:,21) = 1.3027 -3.0669 -2.5140 2.5249 0.3256 1.0849 -2.2210 2.2105 1.0322 -2.7332 1.7464 -1.3768 -0.8949 0.7157 1.6362 0.1681 -1.2050 3.8122 0.3636 1.5360 0.5076 1.6824 -1.2537 -0.1030 0.7563 B(:,:,22) = -0.7558 -1.0014 1.8700 1.9205 -0.6140 0.2165 1.6217 -0.8111 1.0028 2.0232 -2.3559 2.7258 1.7126 -1.8611 0.4371 1.6219 0.7750 -2.5612 0.3213 -2.5177 0.1565 -0.5339 -0.5733 -0.2441 -2.5021 B(:,:,23) = 0.9844 0.8369 0.8553 0.2370 1.1270 2.9168 -0.6636 -0.2539 -0.4327 -0.6501 2.1469 1.4433 1.6019 0.9948 0.2525 1.5140 1.5396 0.9000 0.5947 1.4490 1.0458 0.0183 -1.2318 -0.2988 -2.0634 B(:,:,24) = -0.2771 -1.4910 -2.9626 -1.7318 2.2095 0.9512 -1.1338 0.6782 0.7795 -0.8880 -0.8533 -2.7780 -2.3788 -2.0778 0.2766 -0.9914 -0.2575 -0.3793 -0.1217 0.1563 -2.4972 1.8518 -2.1511 1.8243 -0.6004 B(:,:,25) = 0.2687 -0.3347 1.1227 -0.0886 -2.6626 0.9024 -0.8702 -0.0905 -0.3697 0.1555 -2.9463 -1.2182 -1.1932 0.9498 1.0114 0.2617 0.2796 -0.4914 3.5571 -1.1028 -1.6009 -1.5878 1.3678 -1.3583 -2.3621 B(:,:,26) = 1.2563 0.8366 0.8002 0.6838 0.2330 1.1104 -0.0262 -2.4385 0.4708 -1.7288 -0.1345 0.2320 -1.4339 -0.4139 0.7586 1.0655 2.9163 -0.8192 1.4195 0.5019 1.6090 0.7177 0.0177 1.0356 -2.0008 B(:,:,27) = 1.6343 1.0184 0.8376 -0.9728 0.3353 -2.7207 -0.5291 -0.2911 -1.0033 2.2731 -0.7506 0.7822 2.3866 1.6515 -2.9627 -0.4358 -0.4526 3.2547 -2.1035 -2.0407 0.8323 1.2352 -0.3457 1.4855 0.3752 B(:,:,28) = 1.8239 -0.1335 1.4915 -1.0550 -0.5938 3.7806 0.2710 0.9868 -2.0289 -1.1497 -1.0458 0.4945 0.3546 -0.0739 -1.6005 -0.2407 2.5159 -0.2250 0.5005 1.1066 0.2643 -0.0131 1.3026 1.7734 -0.5729 B(:,:,29) = -0.3810 0.8987 -2.2175 -0.4167 0.5621 -0.5270 0.4304 1.0761 0.1885 0.9786 1.7019 1.0872 -0.9485 0.5736 -2.1015 -1.8118 1.4298 -0.1435 0.7297 0.8214 0.2799 -2.1276 0.8208 -0.1314 -3.5065 B(:,:,30) = -0.1133 -2.7520 -3.2753 -1.0057 -0.8834 0.2685 0.9265 2.2004 -0.9980 0.8306 0.0608 0.3621 -1.8136 0.5018 -3.0189 -1.1764 2.5815 0.0425 -2.5444 -1.3692 -1.1503 -0.2703 -1.2848 -0.1888 -1.1203 B(:,:,31) = -0.6610 -0.8840 0.8206 -0.4026 -0.1174 0.9512 2.4037 1.1887 2.1854 0.1708 1.4476 -1.4069 -2.0353 -2.0397 4.2878 -2.4774 -0.8173 -2.1959 -2.1793 -0.7268 1.2948 0.3052 2.3863 -1.2332 -1.6261 B(:,:,32) = -0.4802 -0.6304 -2.3091 -0.7794 1.3444 2.2289 0.2194 1.5060 0.0024 1.3949 -1.2486 -0.4703 2.1090 0.6853 -1.4494 5.4418 -1.3608 2.3341 -0.5118 5.2558 1.0397 1.5947 -1.6032 -3.4416 2.3021 B(:,:,33) = -1.9845 0.9943 1.9642 -0.4459 0.8147 1.7308 -3.0916 -3.4756 3.4416 4.2824 -1.0330 3.4350 -3.7624 1.5342 -0.7833 -1.1288 0.0081 -2.4436 -2.2191 -1.0281 0.3592 -1.9227 -1.7233 0.3510 1.2419
  2 个评论
Image Analyst
Image Analyst 2022-8-16
To make it more general and robust, you'd do
A = randn(5, 5, 99);
[rows, columns, slices] = size(A)
B = zeros(rows, columns, slices/3);
for k = 1 : slices/3 % or 1 : size(B, 3)
B(:, :, k) = sum(A(:, :, (k - 1) * 3 + (1 : 3)), 3);
end
whos
B
We don't recommend using i (the imaginary variable) as a loop counter, and it's more robust to preallocate the size of B based on the size of A. You might also check in advance if the number of slices is not a multiple of 3 and throw up a friendlier error if it's not.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2022-8-16
OK, so go ahead. I know you did not actually ask a question, but this is how you must proceed.
sum1 = (A1+A2+A3) ;
sum2 = (A4+A5+A6);
% Etc
sum33 = (A97+A98+A99);
Yes, you'll have 33 lines of code rather than a small, compact for loop, but that is the penalty you must pay for unwisely having 99 individual, separately named variables instead of having a higher dimensional array.
  3 个评论

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by