Rewriting code without loops

1 view (last 30 days)
Valeri Aronov
Valeri Aronov on 24 Aug 2021
Edited: Valeri Aronov on 25 Aug 2021
I have the following sizes of arrays of doubles:
dev 101x1
GradW 4x101
HessW 4x4x101
The code in question:
Hess=zeros(4);
for i=1:101
for j=1:4
for k=1:4
Hess(j,k) = Hess(j,k) + 2*(HessW(j,k,i)*dev(i) + GradW(j,i)*GradW(k,i));
% Incorrect original line:
%Hess(j,k,i) = Hess(j,k,i) + 2*(HessW(j,k,i)*dev(i) + GradW(j,i)*GradW(k,i));
end
end
end
Thanks.

Accepted Answer

Stephen23
Stephen23 on 24 Aug 2021
Edited: Stephen23 on 24 Aug 2021
Fake data:
dev = rand(101,1);
GradW = rand(4,101);
HessW = rand(4,4,101);
Your code:
A = zeros(4,4,101);
for i=1:101
for j=1:4
for k=1:4
A(j,k,i) = A(j,k,i) + 2*(HessW(j,k,i)*dev(i) + GradW(j,i)*GradW(k,i));
end % ^^^^^^^^ this contributes nothing
end
end
A
A =
A(:,:,1) = 2.5836 0.9044 1.4959 1.8473 0.7823 0.6776 1.1429 1.3505 1.9823 1.4233 1.7513 0.6737 1.2615 0.5894 1.3237 1.2986 A(:,:,2) = 1.3811 1.1632 0.9150 0.3689 1.6995 2.2056 1.4826 0.3051 1.4785 1.5396 1.4172 0.5296 0.2813 0.5513 0.2217 0.7351 A(:,:,3) = 0.4731 0.8374 1.2685 1.1144 1.2521 1.8853 2.0901 1.6882 1.0772 1.9522 1.9438 2.0023 0.9693 1.9290 1.9730 1.9462 A(:,:,4) = 0.8233 1.0352 1.0343 0.2371 1.5791 0.9710 1.9243 2.3153 0.4584 1.2466 3.0966 2.2308 1.2773 2.4145 2.2315 2.5613 A(:,:,5) = 0.5761 0.4061 0.3191 1.1031 0.5424 2.4394 2.0155 1.4020 0.4571 2.8734 2.6238 1.4002 0.8560 1.6954 1.7577 0.6343 A(:,:,6) = 0.4633 0.4792 0.1727 0.3082 0.4923 0.5321 0.3478 0.6607 0.2281 0.6190 0.3413 0.2459 0.0934 0.8383 0.1771 0.3703 A(:,:,7) = 0.3338 0.4418 0.4568 0.0920 0.1878 0.5939 1.0094 0.4938 0.3521 1.0871 1.6201 0.4900 0.0998 0.3015 0.5136 0.3598 A(:,:,8) = 0.9590 1.8273 0.2302 1.6682 1.3053 2.1319 0.7481 1.6076 0.2310 1.0825 0.1750 0.7549 1.0172 1.6925 0.8151 1.6045 A(:,:,9) = 2.4251 1.4967 1.7850 1.8278 1.5272 0.6467 1.2051 0.2375 2.1103 1.3409 1.8659 1.3775 2.4517 1.0802 1.8180 2.6765 A(:,:,10) = 0.4653 0.6170 0.2271 0.7354 0.3703 1.7364 1.0904 1.6565 0.5914 0.9639 0.2458 0.3748 0.7477 1.4628 0.8076 0.5527 A(:,:,11) = 0.7935 0.5564 0.6957 1.4893 0.7260 0.6079 0.6509 1.2110 0.5056 0.4301 0.4233 1.0352 1.2964 0.8920 1.1383 2.1160 A(:,:,12) = 0.9140 1.7918 1.4235 1.1443 0.6575 2.3480 1.8598 2.4094 0.3258 1.3009 0.4580 1.4516 1.1130 2.0187 1.7732 1.3276 A(:,:,13) = 0.5414 2.2846 0.7604 0.8022 1.9214 1.1060 1.3832 1.4181 1.8788 1.0888 1.8406 0.4387 0.1296 1.7113 0.5100 0.5087 A(:,:,14) = 1.6179 0.8262 0.8502 0.5625 0.4753 1.6023 1.0061 0.3870 1.0189 0.2471 0.9029 2.6158 0.3504 0.9718 1.4946 1.0835 A(:,:,15) = 2.0441 2.2338 1.7646 1.2384 1.8192 2.5335 0.9375 0.8472 0.6771 1.0604 0.6446 1.2863 0.9278 0.8627 0.8340 1.0720 A(:,:,16) = 1.2790 1.0666 2.1268 1.2198 1.0107 1.8457 1.2394 2.0564 1.3217 1.1350 1.8873 2.3869 1.5145 0.5532 0.9508 2.2210 A(:,:,17) = 0.3907 0.5155 0.6554 0.8760 0.4199 0.9919 0.9928 1.1267 0.4856 0.6551 0.3166 0.4802 0.3763 1.0343 0.5581 1.0640 A(:,:,18) = 2.1705 1.2692 1.9351 1.3478 0.7612 1.0312 0.8985 1.3259 2.1529 1.1892 3.3083 0.9480 0.6670 0.5456 1.4710 0.1067 A(:,:,19) = 2.4325 2.5229 1.9239 0.9192 2.6148 2.3679 1.9755 1.4597 2.0593 2.7311 2.0474 1.4357 1.3075 1.0888 0.8247 0.6716 A(:,:,20) = 1.5028 0.5201 0.4771 0.1242 0.5431 0.2607 0.1294 0.1771 0.5074 0.2692 0.2052 0.1740 0.0256 0.0821 0.1592 0.0723 A(:,:,21) = 0.3560 0.4660 0.3148 0.4514 0.4275 1.8900 1.4077 1.1442 0.3277 1.5181 1.3057 0.8563 0.5911 1.3621 0.8676 0.7352 A(:,:,22) = 0.3145 0.1391 0.3320 0.3304 0.3419 0.3748 0.4978 0.1781 0.3253 0.7507 1.0957 0.5005 0.2869 0.3064 0.6136 0.1070 A(:,:,23) = 1.7380 0.9154 1.0392 0.6810 0.9546 1.5780 1.2225 1.3798 1.3154 1.3078 1.6130 1.3654 0.3959 0.7625 0.2358 0.2895 A(:,:,24) = 2.3474 0.9262 1.7764 1.0122 1.9605 1.3162 1.1875 2.1731 0.8245 2.1849 1.9590 1.2512 1.9600 0.7072 1.0694 1.4235 A(:,:,25) = 2.3369 1.3903 1.5711 0.8861 1.1752 1.3573 0.4314 0.3682 0.4910 1.5696 1.0060 0.9460 0.7722 0.6645 1.0035 1.2326 A(:,:,26) = 0.0842 0.0518 0.0943 0.0111 0.0246 0.0237 0.0502 0.0267 0.1273 0.0463 0.1486 0.0452 0.0397 0.0122 0.0431 0.0340 A(:,:,27) = 1.9814 0.3389 0.7829 1.2235 0.4730 0.8422 1.2575 1.3604 2.0179 0.1592 1.4648 1.7976 1.0558 0.6255 1.3969 0.7004 A(:,:,28) = 1.4283 0.3775 1.5352 0.1377 0.3739 0.0913 0.3963 0.0727 1.5976 0.4569 1.6808 0.1890 0.1702 0.0701 0.1486 0.0369 A(:,:,29) = 0.9892 0.1631 0.7192 0.0526 0.7880 0.6647 0.3559 1.0298 0.0697 0.3528 0.2414 0.6115 0.8186 0.2930 0.0940 0.5728 A(:,:,30) = 2.0950 1.9611 1.5992 1.6295 1.8349 1.2682 1.7866 0.5953 2.3706 2.2684 2.3527 1.4831 0.8927 1.7138 2.0292 0.5204 A(:,:,31) = 0.8117 1.0196 1.7942 0.6313 1.3718 2.3230 2.6466 1.1641 0.9590 2.5019 1.7100 0.1436 1.2310 1.3844 1.1840 0.2746 A(:,:,32) = 1.7085 1.1605 1.4811 2.0657 0.8375 0.1839 0.6697 1.6373 1.5472 0.8511 1.6667 1.8346 1.6055 0.6555 1.4956 2.8671 A(:,:,33) = 2.3174 1.9922 2.0402 2.8116 1.4579 1.1073 1.7445 0.8129 1.5762 1.0580 2.2048 2.2927 2.5060 1.8415 2.4554 3.3177 A(:,:,34) = 1.2478 1.3759 0.6779 0.9214 1.9021 2.3756 0.9210 1.0086 0.6739 0.9048 0.0922 0.1427 0.9151 0.8837 0.1190 0.2007 A(:,:,35) = 1.1149 0.8697 0.5973 0.7002 0.7570 0.4851 0.7496 0.2353 0.6868 0.6889 0.4884 0.5210 0.6711 0.4570 0.3708 0.5845 A(:,:,36) = 1.0648 0.3978 0.7970 0.5604 0.4918 0.1583 0.1719 0.2727 0.6647 0.3166 0.5611 0.2732 0.4856 0.0851 0.2113 0.1848 A(:,:,37) = 1.5515 0.3595 0.6171 1.3472 1.1628 0.0561 0.9785 0.3211 0.8815 0.3069 0.8547 0.1934 0.8989 0.7942 0.4371 1.2807 A(:,:,38) = 0.6674 0.7736 0.4546 0.5284 0.3752 0.2712 0.4938 1.3673 1.1331 0.6805 0.1743 1.1409 0.4881 0.4810 1.3868 2.0990 A(:,:,39) = 0.1235 0.2614 0.2983 0.1771 0.0388 0.0976 0.2538 0.1308 0.0969 0.2604 0.8005 0.6210 0.1869 0.2267 0.6652 0.4930 A(:,:,40) = 0.0849 0.6238 0.4638 0.4619 0.3020 1.6581 1.3231 1.2717 0.4985 1.4494 1.2749 1.4048 0.4092 1.3980 1.3742 1.3006 A(:,:,41) = 2.1595 1.1535 2.1982 1.3805 2.1844 1.7889 2.2067 2.0869 1.4479 3.0965 2.0427 3.4957 1.2077 2.9061 1.9868 3.4018 A(:,:,42) = 0.6519 0.6739 0.5241 0.6258 1.0004 1.2478 0.9717 1.0287 0.5812 0.8804 0.7954 0.6621 0.7468 0.7776 0.7115 0.6317 A(:,:,43) = 2.3536 1.5854 1.8984 1.6279 2.1552 1.5884 1.8486 0.7160 2.0977 1.2067 1.6007 0.9798 1.3657 1.0648 1.1441 0.4379 A(:,:,44) = 1.7716 1.4349 0.9809 1.6882 1.2267 0.8592 0.7291 1.2972 1.0698 0.9157 0.8628 1.1585 1.5465 1.2292 1.1571 1.4630 A(:,:,45) = 1.2874 1.5075 1.3051 1.1262 2.0555 1.2190 1.4836 2.0213 1.1458 0.7246 0.5062 0.7575 1.6451 1.6530 1.0859 1.2660 A(:,:,46) = 1.0457 1.3730 1.1529 0.1920 0.4372 1.5368 1.5592 1.6697 0.3681 0.7455 0.6567 1.3584 1.3546 0.4280 1.0270 1.1284 A(:,:,47) = 0.1756 0.0675 0.8160 0.4708 0.7104 0.3387 0.4537 0.2431 0.3051 1.0425 1.5902 0.4328 0.3897 0.2667 1.7242 2.0594 A(:,:,48) = 2.4358 2.1686 2.2577 1.5320 2.2695 1.5701 0.6479 0.5120 1.3578 2.1087 0.3481 1.0209 1.3932 0.2656 1.6916 1.8506 A(:,:,49) = 1.4671 1.5389 2.0187 1.7769 2.7409 1.8573 2.6288 2.0123 1.1780 1.7877 1.2619 0.6754 1.2633 1.2359 1.6436 0.3604 A(:,:,50) = 0.4016 0.4902 1.0979 0.5789 0.7920 1.9733 1.9205 2.5059 0.3143 2.0412 1.4318 2.3942 1.4755 1.3692 1.3427 2.1622 A(:,:,51) = 0.7398 0.9482 0.2748 0.5066 0.1363 0.0144 0.7876 0.9312 0.6008 1.0718 1.1387 0.9971 0.9132 0.3682 0.9032 0.6664 A(:,:,52) = 0.3899 0.4167 0.6424 0.6505 0.6775 0.7540 0.0809 1.1802 0.0323 0.2669 0.4776 0.5590 0.8958 1.0277 0.4558 1.3798 A(:,:,53) = 2.3859 2.0974 1.4847 1.3970 1.8338 2.0937 1.4417 0.8293 0.8081 1.1181 1.3064 1.2590 1.1369 0.9064 0.8585 0.9468 A(:,:,54) = 2.7325 1.7344 1.1780 0.6455 1.4358 1.5394 1.4894 1.6937 2.3756 1.0961 1.3637 1.7483 1.6027 1.2106 0.8504 1.2980 A(:,:,55) = 1.3356 1.3732 1.5806 0.9266 1.0750 0.9771 0.9642 1.6468 1.0022 1.4021 1.8410 0.4533 1.5823 0.4845 0.6622 2.1268 A(:,:,56) = 2.5860 0.3265 0.0933 1.3072 0.4253 0.3614 1.7780 0.3144 1.3971 1.2248 1.6830 0.8657 1.8076 1.5598 0.0073 0.6455 A(:,:,57) = 0.4980 0.2383 0.4720 0.2088 0.2153 1.9796 0.8338 1.8640 0.6758 1.0255 1.0953 1.4595 0.1348 2.1289 1.4650 2.1753 A(:,:,58) = 0.2153 0.1614 0.3004 0.3510 0.3818 0.9424 1.1579 1.1680 0.5428 1.2781 1.4524 1.3233 0.2689 0.9202 1.3249 1.1343 A(:,:,59) = 0.4951 0.7483 0.7696 0.7230 0.9037 0.8538 0.3396 0.8543 0.2397 0.9526 0.7193 0.6756 0.3904 1.0879 0.9588 0.9318 A(:,:,60) = 1.2813 1.0565 1.2865 1.2075 1.5035 1.2939 0.3142 1.5680 0.8916 0.8310 1.2917 1.1334 1.0767 0.9090 2.2175 1.7590 A(:,:,61) = 1.7376 1.2327 0.8761 0.7822 1.2266 0.9989 0.2754 0.8461 0.5941 0.3427 0.8394 0.8455 1.1687 0.3269 0.2148 0.2825 A(:,:,62) = 0.3041 0.6407 0.6489 0.6109 0.8120 2.3020 0.5605 1.5182 0.3789 0.3208 0.6380 0.1652 0.8426 1.4419 0.2053 0.9871 A(:,:,63) = 2.1603 1.2138 2.1344 1.5652 1.4157 1.7371 1.9577 1.5665 2.5072 1.0140 3.3336 2.4727 1.8879 2.0844 2.0024 3.1386 A(:,:,64) = 0.9732 1.9665 1.7272 1.2930 1.4489 1.5525 0.6037 0.5312 2.3260 1.7674 2.1765 1.3347 0.7028 1.3956 1.4599 0.4428 A(:,:,65) = 2.9326 0.6732 2.1792 1.2905 1.9075 1.6430 0.6583 0.5894 2.5937 0.2923 1.6867 2.0431 0.9676 0.1627 0.4248 0.7696 A(:,:,66) = 2.0614 2.2483 2.6693 2.6468 2.3156 2.2308 0.7399 1.0923 1.0985 1.1333 1.6663 2.0195 2.4354 1.1350 1.2054 2.5310 A(:,:,67) = 0.8367 1.8840 2.1350 1.5060 2.0820 1.4190 2.4678 1.2641 1.7913 1.8928 1.0740 1.4176 0.9732 1.0161 0.9923 1.2918 A(:,:,68) = 0.1949 0.1514 0.4376 0.5807 0.1182 0.0751 0.2956 0.3821 0.4200 0.3083 1.0996 1.4754 0.5499 0.3924 1.4932 1.9762 A(:,:,69) = 1.9263 0.5973 1.1265 1.2437 0.7457 0.2509 0.2380 0.5529 1.1466 0.2988 0.4971 0.7708 1.3770 0.4735 0.7595 0.6806 A(:,:,70) = 0.2398 0.7589 1.3702 0.2150 1.3006 2.1404 1.8469 0.9717 1.1448 2.1243 2.4601 1.0517 0.4927 1.3088 0.5677 0.6013 A(:,:,71) = 1.7013 0.3781 1.7111 0.4411 1.2326 0.2912 1.1930 0.2006 0.3566 1.5096 1.6361 1.3674 0.3959 0.8727 0.8172 0.7689 A(:,:,72) = 2.7092 1.6071 2.1053 1.3910 1.7130 1.3880 1.7372 0.9247 2.3691 1.5646 2.1564 1.3994 1.9698 0.9483 1.1895 2.0215 A(:,:,73) = 1.8659 1.3978 0.6228 0.7775 1.0386 0.5624 0.8251 1.8659 0.6047 1.6682 1.7495 1.3224 1.0027 0.7390 1.9242 1.9099 A(:,:,74) = 1.9945 0.8861 0.9356 1.4851 0.5775 0.3946 0.3157 0.4702 0.8739 0.6571 0.4900 0.8189 1.3552 0.6017 0.8192 1.0340 A(:,:,75) = 1.4603 0.9258 0.8691 1.3822 1.8113 0.5527 1.4801 1.3117 0.8810 2.2704 2.8058 1.7037 1.7756 2.2374 3.0832 2.3964 A(:,:,76) = 0.2632 0.4967 0.4348 0.1644 0.5216 1.0399 0.8949 0.2780 0.4221 0.8535 0.7503 0.2309 0.1427 0.2513 0.2460 0.0574 A(:,:,77) = 1.1735 0.8378 1.4701 1.6099 1.3189 1.1564 1.0914 1.0056 1.4060 1.2043 1.8379 1.9657 1.3906 1.0072 1.3794 1.4023 A(:,:,78) = 0.5344 0.7209 0.2462 0.4619 0.7221 0.9728 0.3329 0.6228 0.2478 0.3339 0.1153 0.2139 0.4627 0.6211 0.2126 0.3966 A(:,:,79) = 0.4589 0.8335 0.5571 1.2257 1.2043 1.1590 1.6860 0.6066 0.9289 1.7986 1.9314 0.6161 0.6871 1.4109 1.3938 0.2105 A(:,:,80) = 0.2876 1.4297 0.7852 0.5089 0.3782 1.5391 0.8517 0.1165 0.5459 0.8578 1.1292 0.8072 0.9748 0.4360 1.0994 0.3515 A(:,:,81) = 0.9838 0.6501 0.7191 0.5071 0.3479 1.1206 0.4010 0.7453 0.4595 0.7979 0.9703 0.0819 0.7983 0.6341 0.4368 0.5298 A(:,:,82) = 0.5784 1.2590 1.5596 1.0087 0.5512 0.3156 0.8979 1.3830 1.5575 1.0959 1.5494 2.2348 1.4941 1.8330 1.6948 3.0182 A(:,:,83) = 1.5063 0.6080 0.8097 1.0013 0.1565 1.3161 0.8376 0.7677 1.4873 1.2981 1.9396 1.1794 1.2504 0.2944 1.2614 0.6091 A(:,:,84) = 0.8804 0.6795 1.2551 0.2789 1.0660 1.3858 1.0146 0.6456 1.2736 1.2817 1.2559 0.7488 0.5427 0.8891 0.6280 0.4698 A(:,:,85) = 0.1454 0.4039 0.3723 0.0259 0.3224 0.2349 0.1584 0.1201 0.0694 0.2233 1.6740 1.8481 0.1647 0.3963 1.7897 1.9519 A(:,:,86) = 2.2590 0.3987 1.9323 2.7384 1.5717 0.8299 1.3731 1.1697 3.0720 1.5895 3.1730 2.0995 2.2411 1.9345 2.8650 2.0371 A(:,:,87) = 1.8929 0.4907 0.3797 1.8415 0.3402 0.4181 0.0889 0.6321 0.3754 0.3865 0.1108 0.4344 1.8437 0.3793 0.2143 2.0392 A(:,:,88) = 0.9169 1.0890 1.3252 2.1624 1.9089 2.9189 2.3466 1.9061 1.5390 2.2872 1.3388 2.3223 1.3975 2.8825 1.5359 2.4504 A(:,:,89) = 0.9702 1.5237 1.2184 1.4441 1.0110 1.8937 1.0020 1.9791 0.8998 1.7684 1.2280 1.8167 0.7082 1.5684 1.8149 1.6363 A(:,:,90) = 1.9959 2.0798 2.8422 1.9789 1.5765 0.3297 1.6460 0.8749 2.8594 1.8870 2.9016 1.5873 2.5669 0.9395 2.0915 1.2849 A(:,:,91) = 1.2439 1.5135 0.4137 0.5421 1.5107 1.8612 0.5159 0.6386 0.4202 0.5057 0.1516 0.1868 0.5207 0.6516 0.1781 0.2222 A(:,:,92) = 0.2116 1.0236 0.4345 0.6645 0.7175 1.6502 1.7971 1.4778 0.0917 1.4072 0.8136 0.5245 0.7214 1.1502 0.7922 1.0790 A(:,:,93) = 1.3964 0.8563 0.7295 1.3087 0.8071 0.3871 0.1574 0.7023 0.4681 0.5146 0.3743 0.6861 1.2624 0.6919 0.6392 2.0339 A(:,:,94) = 0.5377 0.4389 0.4432 0.5677 0.3653 1.4126 0.7889 1.3254 0.6117 1.1914 0.6271 1.6403 0.5805 1.5091 1.5939 2.2335 A(:,:,95) = 0.0970 0.3544 1.1649 0.9517 0.3560 1.1806 0.3709 1.2692 1.0718 0.8122 0.8645 0.6964 1.1896 1.6743 1.0948 1.1524 A(:,:,96) = 0.1291 0.1371 0.3220 0.3094 0.1408 0.2733 0.3684 0.4257 0.3539 0.2794 0.1650 0.3252 0.1696 0.2340 0.3299 0.3748 A(:,:,97) = 0.6760 0.4337 0.3301 0.5345 0.3232 0.6182 0.5327 0.5097 0.6247 0.9081 0.4451 0.6233 0.6125 0.6670 0.7980 0.3750 A(:,:,98) = 2.9465 0.5438 1.9928 1.3539 0.3257 0.1459 0.7735 1.0933 1.4552 1.1570 1.6066 0.9073 1.1437 0.9099 1.0334 0.7789 A(:,:,99) = 0.5878 1.0146 0.4398 1.5512 1.4025 1.5832 1.2965 2.2022 0.5524 0.7362 0.9258 0.8824 1.4678 2.0322 0.8319 2.4101 A(:,:,100) = 0.8745 1.3342 0.8575 0.6613 1.3606 1.5569 2.4746 1.6771 0.6767 1.3402 1.3570 0.6837 0.8539 1.9004 2.1270 2.0401 A(:,:,101) = 2.0945 1.9138 2.2850 2.1521 2.6359 2.0269 2.1401 1.1930 2.2826 1.9996 2.0486 1.1710 1.4173 1.5227 0.8006 1.2055
Vectorized code:
B = 2*(HessW.*reshape(dev,1,1,101) + reshape(GradW,4,1,101).*reshape(GradW,1,4,101))
B =
B(:,:,1) = 2.5836 0.9044 1.4959 1.8473 0.7823 0.6776 1.1429 1.3505 1.9823 1.4233 1.7513 0.6737 1.2615 0.5894 1.3237 1.2986 B(:,:,2) = 1.3811 1.1632 0.9150 0.3689 1.6995 2.2056 1.4826 0.3051 1.4785 1.5396 1.4172 0.5296 0.2813 0.5513 0.2217 0.7351 B(:,:,3) = 0.4731 0.8374 1.2685 1.1144 1.2521 1.8853 2.0901 1.6882 1.0772 1.9522 1.9438 2.0023 0.9693 1.9290 1.9730 1.9462 B(:,:,4) = 0.8233 1.0352 1.0343 0.2371 1.5791 0.9710 1.9243 2.3153 0.4584 1.2466 3.0966 2.2308 1.2773 2.4145 2.2315 2.5613 B(:,:,5) = 0.5761 0.4061 0.3191 1.1031 0.5424 2.4394 2.0155 1.4020 0.4571 2.8734 2.6238 1.4002 0.8560 1.6954 1.7577 0.6343 B(:,:,6) = 0.4633 0.4792 0.1727 0.3082 0.4923 0.5321 0.3478 0.6607 0.2281 0.6190 0.3413 0.2459 0.0934 0.8383 0.1771 0.3703 B(:,:,7) = 0.3338 0.4418 0.4568 0.0920 0.1878 0.5939 1.0094 0.4938 0.3521 1.0871 1.6201 0.4900 0.0998 0.3015 0.5136 0.3598 B(:,:,8) = 0.9590 1.8273 0.2302 1.6682 1.3053 2.1319 0.7481 1.6076 0.2310 1.0825 0.1750 0.7549 1.0172 1.6925 0.8151 1.6045 B(:,:,9) = 2.4251 1.4967 1.7850 1.8278 1.5272 0.6467 1.2051 0.2375 2.1103 1.3409 1.8659 1.3775 2.4517 1.0802 1.8180 2.6765 B(:,:,10) = 0.4653 0.6170 0.2271 0.7354 0.3703 1.7364 1.0904 1.6565 0.5914 0.9639 0.2458 0.3748 0.7477 1.4628 0.8076 0.5527 B(:,:,11) = 0.7935 0.5564 0.6957 1.4893 0.7260 0.6079 0.6509 1.2110 0.5056 0.4301 0.4233 1.0352 1.2964 0.8920 1.1383 2.1160 B(:,:,12) = 0.9140 1.7918 1.4235 1.1443 0.6575 2.3480 1.8598 2.4094 0.3258 1.3009 0.4580 1.4516 1.1130 2.0187 1.7732 1.3276 B(:,:,13) = 0.5414 2.2846 0.7604 0.8022 1.9214 1.1060 1.3832 1.4181 1.8788 1.0888 1.8406 0.4387 0.1296 1.7113 0.5100 0.5087 B(:,:,14) = 1.6179 0.8262 0.8502 0.5625 0.4753 1.6023 1.0061 0.3870 1.0189 0.2471 0.9029 2.6158 0.3504 0.9718 1.4946 1.0835 B(:,:,15) = 2.0441 2.2338 1.7646 1.2384 1.8192 2.5335 0.9375 0.8472 0.6771 1.0604 0.6446 1.2863 0.9278 0.8627 0.8340 1.0720 B(:,:,16) = 1.2790 1.0666 2.1268 1.2198 1.0107 1.8457 1.2394 2.0564 1.3217 1.1350 1.8873 2.3869 1.5145 0.5532 0.9508 2.2210 B(:,:,17) = 0.3907 0.5155 0.6554 0.8760 0.4199 0.9919 0.9928 1.1267 0.4856 0.6551 0.3166 0.4802 0.3763 1.0343 0.5581 1.0640 B(:,:,18) = 2.1705 1.2692 1.9351 1.3478 0.7612 1.0312 0.8985 1.3259 2.1529 1.1892 3.3083 0.9480 0.6670 0.5456 1.4710 0.1067 B(:,:,19) = 2.4325 2.5229 1.9239 0.9192 2.6148 2.3679 1.9755 1.4597 2.0593 2.7311 2.0474 1.4357 1.3075 1.0888 0.8247 0.6716 B(:,:,20) = 1.5028 0.5201 0.4771 0.1242 0.5431 0.2607 0.1294 0.1771 0.5074 0.2692 0.2052 0.1740 0.0256 0.0821 0.1592 0.0723 B(:,:,21) = 0.3560 0.4660 0.3148 0.4514 0.4275 1.8900 1.4077 1.1442 0.3277 1.5181 1.3057 0.8563 0.5911 1.3621 0.8676 0.7352 B(:,:,22) = 0.3145 0.1391 0.3320 0.3304 0.3419 0.3748 0.4978 0.1781 0.3253 0.7507 1.0957 0.5005 0.2869 0.3064 0.6136 0.1070 B(:,:,23) = 1.7380 0.9154 1.0392 0.6810 0.9546 1.5780 1.2225 1.3798 1.3154 1.3078 1.6130 1.3654 0.3959 0.7625 0.2358 0.2895 B(:,:,24) = 2.3474 0.9262 1.7764 1.0122 1.9605 1.3162 1.1875 2.1731 0.8245 2.1849 1.9590 1.2512 1.9600 0.7072 1.0694 1.4235 B(:,:,25) = 2.3369 1.3903 1.5711 0.8861 1.1752 1.3573 0.4314 0.3682 0.4910 1.5696 1.0060 0.9460 0.7722 0.6645 1.0035 1.2326 B(:,:,26) = 0.0842 0.0518 0.0943 0.0111 0.0246 0.0237 0.0502 0.0267 0.1273 0.0463 0.1486 0.0452 0.0397 0.0122 0.0431 0.0340 B(:,:,27) = 1.9814 0.3389 0.7829 1.2235 0.4730 0.8422 1.2575 1.3604 2.0179 0.1592 1.4648 1.7976 1.0558 0.6255 1.3969 0.7004 B(:,:,28) = 1.4283 0.3775 1.5352 0.1377 0.3739 0.0913 0.3963 0.0727 1.5976 0.4569 1.6808 0.1890 0.1702 0.0701 0.1486 0.0369 B(:,:,29) = 0.9892 0.1631 0.7192 0.0526 0.7880 0.6647 0.3559 1.0298 0.0697 0.3528 0.2414 0.6115 0.8186 0.2930 0.0940 0.5728 B(:,:,30) = 2.0950 1.9611 1.5992 1.6295 1.8349 1.2682 1.7866 0.5953 2.3706 2.2684 2.3527 1.4831 0.8927 1.7138 2.0292 0.5204 B(:,:,31) = 0.8117 1.0196 1.7942 0.6313 1.3718 2.3230 2.6466 1.1641 0.9590 2.5019 1.7100 0.1436 1.2310 1.3844 1.1840 0.2746 B(:,:,32) = 1.7085 1.1605 1.4811 2.0657 0.8375 0.1839 0.6697 1.6373 1.5472 0.8511 1.6667 1.8346 1.6055 0.6555 1.4956 2.8671 B(:,:,33) = 2.3174 1.9922 2.0402 2.8116 1.4579 1.1073 1.7445 0.8129 1.5762 1.0580 2.2048 2.2927 2.5060 1.8415 2.4554 3.3177 B(:,:,34) = 1.2478 1.3759 0.6779 0.9214 1.9021 2.3756 0.9210 1.0086 0.6739 0.9048 0.0922 0.1427 0.9151 0.8837 0.1190 0.2007 B(:,:,35) = 1.1149 0.8697 0.5973 0.7002 0.7570 0.4851 0.7496 0.2353 0.6868 0.6889 0.4884 0.5210 0.6711 0.4570 0.3708 0.5845 B(:,:,36) = 1.0648 0.3978 0.7970 0.5604 0.4918 0.1583 0.1719 0.2727 0.6647 0.3166 0.5611 0.2732 0.4856 0.0851 0.2113 0.1848 B(:,:,37) = 1.5515 0.3595 0.6171 1.3472 1.1628 0.0561 0.9785 0.3211 0.8815 0.3069 0.8547 0.1934 0.8989 0.7942 0.4371 1.2807 B(:,:,38) = 0.6674 0.7736 0.4546 0.5284 0.3752 0.2712 0.4938 1.3673 1.1331 0.6805 0.1743 1.1409 0.4881 0.4810 1.3868 2.0990 B(:,:,39) = 0.1235 0.2614 0.2983 0.1771 0.0388 0.0976 0.2538 0.1308 0.0969 0.2604 0.8005 0.6210 0.1869 0.2267 0.6652 0.4930 B(:,:,40) = 0.0849 0.6238 0.4638 0.4619 0.3020 1.6581 1.3231 1.2717 0.4985 1.4494 1.2749 1.4048 0.4092 1.3980 1.3742 1.3006 B(:,:,41) = 2.1595 1.1535 2.1982 1.3805 2.1844 1.7889 2.2067 2.0869 1.4479 3.0965 2.0427 3.4957 1.2077 2.9061 1.9868 3.4018 B(:,:,42) = 0.6519 0.6739 0.5241 0.6258 1.0004 1.2478 0.9717 1.0287 0.5812 0.8804 0.7954 0.6621 0.7468 0.7776 0.7115 0.6317 B(:,:,43) = 2.3536 1.5854 1.8984 1.6279 2.1552 1.5884 1.8486 0.7160 2.0977 1.2067 1.6007 0.9798 1.3657 1.0648 1.1441 0.4379 B(:,:,44) = 1.7716 1.4349 0.9809 1.6882 1.2267 0.8592 0.7291 1.2972 1.0698 0.9157 0.8628 1.1585 1.5465 1.2292 1.1571 1.4630 B(:,:,45) = 1.2874 1.5075 1.3051 1.1262 2.0555 1.2190 1.4836 2.0213 1.1458 0.7246 0.5062 0.7575 1.6451 1.6530 1.0859 1.2660 B(:,:,46) = 1.0457 1.3730 1.1529 0.1920 0.4372 1.5368 1.5592 1.6697 0.3681 0.7455 0.6567 1.3584 1.3546 0.4280 1.0270 1.1284 B(:,:,47) = 0.1756 0.0675 0.8160 0.4708 0.7104 0.3387 0.4537 0.2431 0.3051 1.0425 1.5902 0.4328 0.3897 0.2667 1.7242 2.0594 B(:,:,48) = 2.4358 2.1686 2.2577 1.5320 2.2695 1.5701 0.6479 0.5120 1.3578 2.1087 0.3481 1.0209 1.3932 0.2656 1.6916 1.8506 B(:,:,49) = 1.4671 1.5389 2.0187 1.7769 2.7409 1.8573 2.6288 2.0123 1.1780 1.7877 1.2619 0.6754 1.2633 1.2359 1.6436 0.3604 B(:,:,50) = 0.4016 0.4902 1.0979 0.5789 0.7920 1.9733 1.9205 2.5059 0.3143 2.0412 1.4318 2.3942 1.4755 1.3692 1.3427 2.1622 B(:,:,51) = 0.7398 0.9482 0.2748 0.5066 0.1363 0.0144 0.7876 0.9312 0.6008 1.0718 1.1387 0.9971 0.9132 0.3682 0.9032 0.6664 B(:,:,52) = 0.3899 0.4167 0.6424 0.6505 0.6775 0.7540 0.0809 1.1802 0.0323 0.2669 0.4776 0.5590 0.8958 1.0277 0.4558 1.3798 B(:,:,53) = 2.3859 2.0974 1.4847 1.3970 1.8338 2.0937 1.4417 0.8293 0.8081 1.1181 1.3064 1.2590 1.1369 0.9064 0.8585 0.9468 B(:,:,54) = 2.7325 1.7344 1.1780 0.6455 1.4358 1.5394 1.4894 1.6937 2.3756 1.0961 1.3637 1.7483 1.6027 1.2106 0.8504 1.2980 B(:,:,55) = 1.3356 1.3732 1.5806 0.9266 1.0750 0.9771 0.9642 1.6468 1.0022 1.4021 1.8410 0.4533 1.5823 0.4845 0.6622 2.1268 B(:,:,56) = 2.5860 0.3265 0.0933 1.3072 0.4253 0.3614 1.7780 0.3144 1.3971 1.2248 1.6830 0.8657 1.8076 1.5598 0.0073 0.6455 B(:,:,57) = 0.4980 0.2383 0.4720 0.2088 0.2153 1.9796 0.8338 1.8640 0.6758 1.0255 1.0953 1.4595 0.1348 2.1289 1.4650 2.1753 B(:,:,58) = 0.2153 0.1614 0.3004 0.3510 0.3818 0.9424 1.1579 1.1680 0.5428 1.2781 1.4524 1.3233 0.2689 0.9202 1.3249 1.1343 B(:,:,59) = 0.4951 0.7483 0.7696 0.7230 0.9037 0.8538 0.3396 0.8543 0.2397 0.9526 0.7193 0.6756 0.3904 1.0879 0.9588 0.9318 B(:,:,60) = 1.2813 1.0565 1.2865 1.2075 1.5035 1.2939 0.3142 1.5680 0.8916 0.8310 1.2917 1.1334 1.0767 0.9090 2.2175 1.7590 B(:,:,61) = 1.7376 1.2327 0.8761 0.7822 1.2266 0.9989 0.2754 0.8461 0.5941 0.3427 0.8394 0.8455 1.1687 0.3269 0.2148 0.2825 B(:,:,62) = 0.3041 0.6407 0.6489 0.6109 0.8120 2.3020 0.5605 1.5182 0.3789 0.3208 0.6380 0.1652 0.8426 1.4419 0.2053 0.9871 B(:,:,63) = 2.1603 1.2138 2.1344 1.5652 1.4157 1.7371 1.9577 1.5665 2.5072 1.0140 3.3336 2.4727 1.8879 2.0844 2.0024 3.1386 B(:,:,64) = 0.9732 1.9665 1.7272 1.2930 1.4489 1.5525 0.6037 0.5312 2.3260 1.7674 2.1765 1.3347 0.7028 1.3956 1.4599 0.4428 B(:,:,65) = 2.9326 0.6732 2.1792 1.2905 1.9075 1.6430 0.6583 0.5894 2.5937 0.2923 1.6867 2.0431 0.9676 0.1627 0.4248 0.7696 B(:,:,66) = 2.0614 2.2483 2.6693 2.6468 2.3156 2.2308 0.7399 1.0923 1.0985 1.1333 1.6663 2.0195 2.4354 1.1350 1.2054 2.5310 B(:,:,67) = 0.8367 1.8840 2.1350 1.5060 2.0820 1.4190 2.4678 1.2641 1.7913 1.8928 1.0740 1.4176 0.9732 1.0161 0.9923 1.2918 B(:,:,68) = 0.1949 0.1514 0.4376 0.5807 0.1182 0.0751 0.2956 0.3821 0.4200 0.3083 1.0996 1.4754 0.5499 0.3924 1.4932 1.9762 B(:,:,69) = 1.9263 0.5973 1.1265 1.2437 0.7457 0.2509 0.2380 0.5529 1.1466 0.2988 0.4971 0.7708 1.3770 0.4735 0.7595 0.6806 B(:,:,70) = 0.2398 0.7589 1.3702 0.2150 1.3006 2.1404 1.8469 0.9717 1.1448 2.1243 2.4601 1.0517 0.4927 1.3088 0.5677 0.6013 B(:,:,71) = 1.7013 0.3781 1.7111 0.4411 1.2326 0.2912 1.1930 0.2006 0.3566 1.5096 1.6361 1.3674 0.3959 0.8727 0.8172 0.7689 B(:,:,72) = 2.7092 1.6071 2.1053 1.3910 1.7130 1.3880 1.7372 0.9247 2.3691 1.5646 2.1564 1.3994 1.9698 0.9483 1.1895 2.0215 B(:,:,73) = 1.8659 1.3978 0.6228 0.7775 1.0386 0.5624 0.8251 1.8659 0.6047 1.6682 1.7495 1.3224 1.0027 0.7390 1.9242 1.9099 B(:,:,74) = 1.9945 0.8861 0.9356 1.4851 0.5775 0.3946 0.3157 0.4702 0.8739 0.6571 0.4900 0.8189 1.3552 0.6017 0.8192 1.0340 B(:,:,75) = 1.4603 0.9258 0.8691 1.3822 1.8113 0.5527 1.4801 1.3117 0.8810 2.2704 2.8058 1.7037 1.7756 2.2374 3.0832 2.3964 B(:,:,76) = 0.2632 0.4967 0.4348 0.1644 0.5216 1.0399 0.8949 0.2780 0.4221 0.8535 0.7503 0.2309 0.1427 0.2513 0.2460 0.0574 B(:,:,77) = 1.1735 0.8378 1.4701 1.6099 1.3189 1.1564 1.0914 1.0056 1.4060 1.2043 1.8379 1.9657 1.3906 1.0072 1.3794 1.4023 B(:,:,78) = 0.5344 0.7209 0.2462 0.4619 0.7221 0.9728 0.3329 0.6228 0.2478 0.3339 0.1153 0.2139 0.4627 0.6211 0.2126 0.3966 B(:,:,79) = 0.4589 0.8335 0.5571 1.2257 1.2043 1.1590 1.6860 0.6066 0.9289 1.7986 1.9314 0.6161 0.6871 1.4109 1.3938 0.2105 B(:,:,80) = 0.2876 1.4297 0.7852 0.5089 0.3782 1.5391 0.8517 0.1165 0.5459 0.8578 1.1292 0.8072 0.9748 0.4360 1.0994 0.3515 B(:,:,81) = 0.9838 0.6501 0.7191 0.5071 0.3479 1.1206 0.4010 0.7453 0.4595 0.7979 0.9703 0.0819 0.7983 0.6341 0.4368 0.5298 B(:,:,82) = 0.5784 1.2590 1.5596 1.0087 0.5512 0.3156 0.8979 1.3830 1.5575 1.0959 1.5494 2.2348 1.4941 1.8330 1.6948 3.0182 B(:,:,83) = 1.5063 0.6080 0.8097 1.0013 0.1565 1.3161 0.8376 0.7677 1.4873 1.2981 1.9396 1.1794 1.2504 0.2944 1.2614 0.6091 B(:,:,84) = 0.8804 0.6795 1.2551 0.2789 1.0660 1.3858 1.0146 0.6456 1.2736 1.2817 1.2559 0.7488 0.5427 0.8891 0.6280 0.4698 B(:,:,85) = 0.1454 0.4039 0.3723 0.0259 0.3224 0.2349 0.1584 0.1201 0.0694 0.2233 1.6740 1.8481 0.1647 0.3963 1.7897 1.9519 B(:,:,86) = 2.2590 0.3987 1.9323 2.7384 1.5717 0.8299 1.3731 1.1697 3.0720 1.5895 3.1730 2.0995 2.2411 1.9345 2.8650 2.0371 B(:,:,87) = 1.8929 0.4907 0.3797 1.8415 0.3402 0.4181 0.0889 0.6321 0.3754 0.3865 0.1108 0.4344 1.8437 0.3793 0.2143 2.0392 B(:,:,88) = 0.9169 1.0890 1.3252 2.1624 1.9089 2.9189 2.3466 1.9061 1.5390 2.2872 1.3388 2.3223 1.3975 2.8825 1.5359 2.4504 B(:,:,89) = 0.9702 1.5237 1.2184 1.4441 1.0110 1.8937 1.0020 1.9791 0.8998 1.7684 1.2280 1.8167 0.7082 1.5684 1.8149 1.6363 B(:,:,90) = 1.9959 2.0798 2.8422 1.9789 1.5765 0.3297 1.6460 0.8749 2.8594 1.8870 2.9016 1.5873 2.5669 0.9395 2.0915 1.2849 B(:,:,91) = 1.2439 1.5135 0.4137 0.5421 1.5107 1.8612 0.5159 0.6386 0.4202 0.5057 0.1516 0.1868 0.5207 0.6516 0.1781 0.2222 B(:,:,92) = 0.2116 1.0236 0.4345 0.6645 0.7175 1.6502 1.7971 1.4778 0.0917 1.4072 0.8136 0.5245 0.7214 1.1502 0.7922 1.0790 B(:,:,93) = 1.3964 0.8563 0.7295 1.3087 0.8071 0.3871 0.1574 0.7023 0.4681 0.5146 0.3743 0.6861 1.2624 0.6919 0.6392 2.0339 B(:,:,94) = 0.5377 0.4389 0.4432 0.5677 0.3653 1.4126 0.7889 1.3254 0.6117 1.1914 0.6271 1.6403 0.5805 1.5091 1.5939 2.2335 B(:,:,95) = 0.0970 0.3544 1.1649 0.9517 0.3560 1.1806 0.3709 1.2692 1.0718 0.8122 0.8645 0.6964 1.1896 1.6743 1.0948 1.1524 B(:,:,96) = 0.1291 0.1371 0.3220 0.3094 0.1408 0.2733 0.3684 0.4257 0.3539 0.2794 0.1650 0.3252 0.1696 0.2340 0.3299 0.3748 B(:,:,97) = 0.6760 0.4337 0.3301 0.5345 0.3232 0.6182 0.5327 0.5097 0.6247 0.9081 0.4451 0.6233 0.6125 0.6670 0.7980 0.3750 B(:,:,98) = 2.9465 0.5438 1.9928 1.3539 0.3257 0.1459 0.7735 1.0933 1.4552 1.1570 1.6066 0.9073 1.1437 0.9099 1.0334 0.7789 B(:,:,99) = 0.5878 1.0146 0.4398 1.5512 1.4025 1.5832 1.2965 2.2022 0.5524 0.7362 0.9258 0.8824 1.4678 2.0322 0.8319 2.4101 B(:,:,100) = 0.8745 1.3342 0.8575 0.6613 1.3606 1.5569 2.4746 1.6771 0.6767 1.3402 1.3570 0.6837 0.8539 1.9004 2.1270 2.0401 B(:,:,101) = 2.0945 1.9138 2.2850 2.1521 2.6359 2.0269 2.1401 1.1930 2.2826 1.9996 2.0486 1.1710 1.4173 1.5227 0.8006 1.2055
Comparison:
isequal(A,B)
ans = logical
1
  4 Comments
Stephen23
Stephen23 on 25 Aug 2021
Fake data:
dev = rand(101,1);
GradW = rand(4,101);
HessW = rand(4,4,101);
Your code:
A = zeros(4,4);
for i=1:101
for j=1:4
for k=1:4
A(j,k) = A(j,k) + 2*(HessW(j,k,i)*dev(i) + GradW(j,i)*GradW(k,i));
end
end
end
A
A = 4×4
115.7081 113.1069 109.1793 108.6062 106.1725 130.8618 110.4741 104.1574 107.9533 112.6988 126.8482 105.9590 100.8884 106.5506 103.3296 117.3484
Vectorized code:
B = HessW.*reshape(dev,1,1,101) + reshape(GradW,4,1,101).*reshape(GradW,1,4,101);
B = sum(B*2,3)
B = 4×4
115.7081 113.1069 109.1793 108.6062 106.1725 130.8618 110.4741 104.1574 107.9533 112.6988 126.8482 105.9590 100.8884 106.5506 103.3296 117.3484
Comparison:
isequal(A,B)
ans = logical
1
Valeri Aronov
Valeri Aronov on 25 Aug 2021
Thanks, Stephen. I wish I can do another 'Accept' for you ...

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!