Cody

Problem 42798. Moving Product (Easy)

Solution 2852435

Submitted on 19 Aug 2020
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test Status Code Input and Output
1   Fail
A = [1 2 3 4 5 6]; C = [6 24 60 120]; assert(isequal(moveprod(A,3),C))

Not enough input arguments. Error in moveprod (line 2) movprod(A,k,dim) Error in Test1 (line 3) assert(isequal(moveprod(A,3),C))

2   Fail
A = [1 2 3 4 5 6].'/6; C = [2 6 12 20 30].'/6^2; B = moveprod(A,2); assert(max(abs(B-C)) <= max(abs(C))*1e-12 && isequal(size(B),size(C)))

Not enough input arguments. Error in moveprod (line 2) movprod(A,k,dim) Error in Test2 (line 3) B = moveprod(A,2);

3   Fail
A = [-1 4 3 6 -2 2 0 1 2 -3 3 -6 -2 3 5]; C = [-2 0 3 12 6 6 0 -2 6 -15]; assert(isequal(moveprod(A,2),C))

Not enough input arguments. Error in moveprod (line 2) movprod(A,k,dim) Error in Test3 (line 6) assert(isequal(moveprod(A,2),C))

4   Fail
A = [-1 4 3 6 2 2 0 1 2 3 3 -6 -2 3 5]/pi; C = [-12 72 36 0 0 6 36 36 -30]/pi^3; B = moveprod(A,3,2); assert(max(abs(B(:) - C(:))) <= max(abs(C(:)))*1e-12 && isequal(size(B),size(C)))

ans = -0.4053 -0.3870 2.3221 1.1611 1.2159 0 0 0 0.1935 0.6079 -1.8238 1.1611 1.1611 -0.9675 1.5198

Matrix dimensions must agree. Error in Test4 (line 8) assert(max(abs(B(:) - C(:))) <= max(abs(C(:)))*1e-12 && isequal(size(B),size(C)))

5   Fail
A = randi([-10,10],10,10,100); k = 5; dim = 3; B = moveprod(A,k,dim); szA = size(A); C = zeros(szA(1),szA(2),szA(3)-k+1); for m = 1:szA(1) for n = 1:szA(2) C(m,n,:) = moveprod(squeeze(A(m,n,:)),k); end end assert(isequal(B,C))

ans(:,:,1) = 300 0 48 -540 -30 90 -30 -42 400 72 243 -40 80 70 -108 -8 -108 -70 42 -36 72 0 -280 24 180 -48 -256 -600 54 24 -192 -42 -108 10 35 144 0 432 0 -64 -45 504 -36 -60 216 252 0 -20 -216 0 -245 270 -80 252 105 -42 -9 180 -48 0 -28 168 -8 15 20 0 140 20 0 175 -18 12 12 72 147 -280 126 48 -1 -392 -120 630 0 3 0 24 56 -18 -168 0 0 350 0 150 216 -36 -140 420 18 490 ans(:,:,2) = 3000 0 -480 2160 30 180 120 378 -800 576 729 -280 -480 -560 756 0 -540 -280 42 -360 -288 0 0 -48 -1800 -432 -2560 3600 540 120 384 -84 0 0 -280 -1152 0 -432 0 576 -135 -504 -180 0 1728 -2016 0 0 0 0 -980 -1080 400 -1764 -945 -168 81 -1440 432 0 112 0 72 -45 -140 0 -980 -80 0 1400 144 60 -60 -504 -735 -2520 -504 -192 -10 2744 960 -6300 0 -30 0 192 -336 18 0 0 0 -2450 0 -1200 -1728 -36 -840 -840 -108 -490 ans(:,:,3) = -24000 0 -960 0 60 540 -1200 -1890 -4800 1152 -4374 1400 -3360 -4480 -6048 0 -5400 2520 210 -1440 -2592 0 0 -192 1800 1728 12800 10800 -1080 -1200 -2688 -336 0 0 -1680 1152 0 432 0 1152 -675 2520 540 0 -12096 -4032 0 0 0 0 4900 0 0 -17640 1890 1680 -486 4320 2160 0 -560 0 144 405 840 0 -3920 160 0 8400 144 -180 60 -2016 3675 -7560 -5040 -768 90 19208 5760 37800 0 270 0 576 -3360 180 0 0 0 -9800 0 2400 0 -108 840 5040 -216 3920 ans(:,:,4) = -24000 0 -480 0 0 180 -1440 4725 -960 288 -1458 420 10080 15680 -12096 0 5400 -11340 -630 4800 -648 0 0 384 1260 -1728 1600 -1080 240 -600 -1008 1680 0 0 -1680 -1920 -11200 96 0 1152 -810 -2240 -1215 0 4032 448 0 0 0 -864 -2100 0 0 0 3150 5600 -378 6480 -1080 0 80 0 -324 -3645 1050 0 2744 640 0 -4800 -96 30 140 -3528 -3150 11340 -5040 2304 810 24010 11520 22680 0 -810 0 -2880 3360 -420 0 0 0 -2800 0 -3360 0 360 -480 2520 252 -4480 ans(:,:,5) = -28000 0 -1440 0 0 -36 -2400 6075 840 1728 -2916 -420 14112 21952 14112 0 6000 22680 -315 -10800 -1080 0 0 -480 1260 1152 -1600 -108 160 -4800 1680 2240 0 0 2160 -4800 39200 -12 0 288 8100 -1920 1215 0 1792 -560 7056 0 0 1440 2400 0 0 0 -900 5600 -1890 -17280 -810 8100 40 0 -2592 -1215 6300 0 -9604 640 0 4800 96 -150 -280 1176 450 16200 -5600 -768 6480 6860 17280 5040 800 -6480 1600 -1440 2400 0 0 0 -16800 -2800 3360 -1344 0 -900 336 420 168 5120 ans(:,:,6) = -50400 15120 -2880 0 0 12 14400 14175 -672 768 648 4200 -5292 3136 -23520 0 10500 25920 -180 0 -2880 0 0 -2400 840 -576 600 -72 800 -1600 2100 1920 0 0 -432 -3200 31360 0 -16000 1296 7200 -960 2430 0 -1792 -160 -8064 0 0 -720 4800 0 0 0 1260 14000 -15120 -6912 0 -16200 -80 0 -7776 1215 5040 0 10976 -64 3600 4800 -32 0 0 784 -300 11340 -4000 960 64800 7840 -3456 -6480 600 -58320 3600 -240 3000 0 0 -5760 -4200 -2240 0 -4480 0 1350 -1680 -180 -672 2560 ans(:,:,7) = -45360 2160 -1152 0 0 -60 -3600 -3150 -1344 -672 864 -3000 -7056 1960 -26880 -28000 16800 0 -180 0 0 0 480 -10800 -504 128 -420 -96 -160 960 0 -1920 -5600 1944 -324 2000 0 0 -12800 144 -21600 -8640 -1458 -540 -1568 200 2016 -6804 15120 -720 -7200 0 0 0 -840 -31500 -10080 -8640 0 -25920 -160 -1620 -6048 2835 6480 0 10976 -144 5400 -6000 4 0 0 560 -420 3780 -5000 1680 -58320 1120 -1296 2592 -2700 -11664 -9000 180 3500 0 1470 -2880 700 640 0 -2800 0 -13500 -2520 810 -672 10240 ans(:,:,8) = 56700 360 4608 -28800 0 60 -1800 -3150 672 1008 -1440 6000 -6048 -735 -10080 8000 -3360 0 -72 0 0 0 0 13500 0 -320 -504 256 -80 -288 0 4800 -11200 1296 -108 10000 0 0 0 -72 -17280 -15552 -2430 90 -224 900 3024 13608 -18900 -1200 4320 0 0 0 -1680 -22050 1680 -14400 0 -25920 -64 -12960 -21168 630 8640 0 -19208 -288 6480 6000 40 0 0 -420 -504 6300 2000 -3780 25920 -960 1512 -432 -1620 2592 -9000 60 1050 0 -1470 -1440 -490 -320 0 -5600 -1200 -22500 17640 405 -672 6400 ans(:,:,9) = -34020 -90 -1152 25920 3456 300 300 -1260 -1680 -1512 960 -8000 -9072 -420 -16800 14000 -2240 0 -120 0 0 0 0 -6750 0 320 -3528 1280 -40 1152 0 8640 -3360 -864 756 7000 0 0 0 36 -28800 3888 1080 45 -784 -6300 -2352 -4536 -18900 900 12960 0 0 -17640 -2352 2205 -1680 -11200 0 -28800 128 25920 2352 -700 ...

Not enough input arguments. Error in moveprod (line 2) movprod(A,k,dim) Error in Test5 (line 8) C(m,n,:) = moveprod(squeeze(A(m,n,:)),k);

6   Fail
A = 20*rand(10,10,100)-10; k = 4; dim = 3; B = moveprod(A,k,dim); szA = size(A); C = zeros(szA(1),szA(2),szA(3)-k+1); for m = 1:szA(1) for n = 1:szA(2) C(m,n,:) = moveprod(squeeze(A(m,n,:)),k); end end C = C + 100*eps(C); assert(max(abs(B(:) - C(:))) <= max(abs(C(:)))*1e-12 && isequal(size(B),size(C)))

ans(:,:,1) = -27.0939 -7.5770 11.5025 7.6747 -76.5443 7.5823 38.7534 -12.9032 -21.5841 7.9109 -47.6005 29.5977 17.5032 -25.9916 -65.3797 26.7616 81.2317 -38.3689 -10.4811 -5.6064 47.4101 -8.5499 -8.2215 4.4187 -24.3281 -6.3572 4.0220 -51.4105 57.8726 -8.7644 -17.6391 26.2863 10.1826 41.7717 -61.1754 8.3303 -3.2149 17.1441 -7.8300 43.9276 -18.7182 30.2464 0.2750 1.7521 -84.1793 -0.1164 -9.1813 0.4155 75.1883 -3.5782 -6.1240 -6.8189 32.3975 13.2122 15.0120 -40.0919 46.1221 -17.0983 6.2738 13.7840 -5.2555 21.4131 -27.4547 -23.8643 -59.5808 -17.5812 -1.7711 0.2008 67.9556 21.5452 -9.6379 2.2139 -6.2617 -59.0547 -9.7537 5.6163 -62.3223 -16.3217 -13.5785 19.5308 35.7506 46.8061 35.9870 -13.5920 8.7481 -82.2205 21.8257 -35.2813 72.3522 1.8081 31.8505 -50.5252 42.5297 42.3841 -20.2740 -8.4454 -3.1376 14.2443 -5.5829 -4.9918 ans(:,:,2) = 69.4044 -63.3527 -48.7704 36.1902 50.7457 -68.4465 -103.4057 -127.3962 -206.6437 -73.3961 -352.8422 -136.7909 -113.7843 212.3605 -629.8773 -37.2461 -704.6459 50.3278 -4.1249 -47.4840 -155.7872 -70.0438 11.0787 37.0411 145.9266 33.7937 9.8236 -428.5680 148.4347 -10.7570 -153.8795 -91.5962 -17.9531 190.8876 292.4342 -70.6561 7.2483 -45.3304 -48.0618 -72.5218 -139.2523 22.2683 1.8455 -5.3533 260.4794 -0.9172 -72.0882 -1.3680 -696.1966 23.1456 33.2511 23.1846 251.8437 -114.5586 -36.5251 -99.4775 9.9099 -151.2584 -53.5049 -10.1738 13.5227 193.6653 -123.4612 -63.5878 -143.7604 -145.7102 9.6101 0.3414 -335.5855 183.9055 -21.0940 -5.0537 0.2257 41.9249 -75.7695 -51.9912 61.3120 -144.8287 -1.1983 -64.2915 187.6035 459.8038 286.6306 90.1212 -45.2103 247.6285 213.7845 204.8444 -8.6330 -16.2331 162.3579 -469.7088 -216.6363 -137.2529 -77.8317 57.4698 -12.0157 92.2523 8.2063 21.9515 ans(:,:,3) = 1.0e+03 * -0.4853 -0.1034 0.4628 0.2054 -0.1306 -0.3856 0.7848 -0.6278 -0.3705 -0.3298 0.6635 0.9589 -0.6324 -0.2505 -1.2327 0.3649 -3.4224 -0.0980 0.0243 -0.1269 -1.0318 -0.1562 0.0043 -0.3365 1.3289 0.3311 -0.0053 0.3009 -0.1682 0.0906 0.3297 -0.5054 -0.1541 -1.7220 0.4701 -0.4520 0.0200 -0.1495 -0.3400 0.0637 0.0917 -0.0758 -0.0070 -0.0263 -2.1247 -0.0009 -0.6790 0.0035 -2.6382 0.2247 -0.1116 0.2057 1.6642 0.3964 0.0925 -0.7714 -0.0314 -0.0561 -0.2492 -0.0933 0.0636 -0.2297 0.4265 0.4560 0.2775 -1.4099 -0.0899 0.0006 1.7255 1.6935 0.0235 0.0001 0.0010 -0.2240 -0.2848 0.4659 0.5548 0.9995 -0.0093 0.6233 -0.2780 -1.4333 -0.3373 -0.3913 -0.2613 0.9612 -1.6936 -1.5053 0.0060 0.0772 -0.9357 1.3432 1.3485 -0.6232 -0.3645 -0.3451 -0.0783 0.8612 -0.0729 -0.1555 ans(:,:,4) = 1.0e+03 * -1.3998 0.0866 -0.6263 0.5591 -0.1382 0.5817 -0.0312 0.6804 0.3398 -0.1828 0.8668 -1.5863 0.5293 -0.3143 0.5350 -0.2055 4.0362 0.0027 -0.0517 -0.2665 0.2331 -0.3011 0.0033 -0.3635 -0.9413 0.5401 -0.0681 -0.2215 -0.1272 0.0002 -0.3075 -0.4045 -0.0400 1.5639 -0.3249 -0.0958 0.0130 0.0972 0.8122 -0.0684 -0.0875 0.2033 -0.0139 0.1674 -0.7916 -0.0001 -0.8566 0.0025 -1.8480 -0.0706 -0.7913 1.5036 -1.1544 -0.7351 -0.0944 -0.3895 0.0270 -0.0303 -0.6267 -0.0380 -0.0411 -0.2578 0.3234 -0.4717 0.2173 -4.6230 -2.0808 0.0001 1.0726 -2.7168 0.0138 0.0001 -0.0011 -0.0643 -0.1534 0.3983 0.2564 -3.1053 -0.0233 -0.7396 -0.0856 -1.7907 0.3660 0.0346 -0.0953 -0.2661 1.5640 0.9054 0.0056 0.8044 0.9239 -1.2476 -2.0542 0.8823 0.2122 -0.1345 0.2698 0.7786 1.1944 -0.6198 ans(:,:,5) = 1.0e+03 * -0.5942 -0.2466 1.4670 -0.8339 0.0301 2.6749 -0.0051 0.0999 -0.0797 -0.1287 -0.9954 1.9192 -0.2747 -0.2012 0.5062 -0.5747 -2.4788 0.0007 -0.1201 0.8358 -0.2462 -0.0695 -0.0112 -0.2557 -0.3229 -1.4567 0.0431 -0.2300 -0.0078 -0.0003 0.2162 0.4981 0.0436 -0.9521 0.3794 -0.0332 -0.2399 -0.0335 -2.5363 -0.0886 0.0174 -0.2258 -0.3459 -0.2172 0.1686 0.0013 0.0896 0.4276 1.3350 1.0722 -0.2225 -0.6102 1.2670 0.4207 -0.0391 -0.6697 -0.0113 -0.0293 -0.7280 0.1156 0.0615 0.2186 -0.0111 -1.0751 -0.0615 0.9514 -2.4187 -0.0116 -0.4207 -1.1828 0.0264 -0.0002 0.0027 0.0427 0.3116 -1.3121 0.1523 -0.3209 -0.0067 -0.0501 -0.0760 1.9490 -0.0726 -0.0733 -0.2265 -0.2427 -0.8655 0.8007 0.0064 -2.7643 -0.1994 -1.4077 -1.7838 0.3864 -0.5975 0.7717 1.0960 -0.4874 0.4738 0.3775 ans(:,:,6) = 1.0e+03 * -1.4990 0.2404 -1.2553 1.5248 -0.4154 -1.4003 0.0082 -0.0899 0.0033 0.1290 -0.9859 2.7112 -0.3137 0.1684 0.1862 1.0915 -0.3088 0.0033 2.9831 0.7178 -0.0291 -0.0788 -0.0668 -0.0787 0.3549 0.7181 0.0033 0.1138 0.0209 -0.0006 -0.1590 -1.1329 0.2308 -0.9250 0.2476 0.0363 0.8626 0.0261 1.4808 0.4768 -0.0036 1.6323 0.4061 0.0891 0.2346 0.0010 0.0038 0.4207 -0.9677 0.0656 -0.4038 1.5936 0.5362 0.4796 0.0819 -0.1509 -0.3501 -0.0018 -0.6228 0.3893 -0.1552 0.0176 -0.0001 0.9031 -0.1949 -0.1116 -1.3543 -0.0554 0.3739 -1.2765 -0.0301 0.0007 -0.3982 -0.1814 0.3980 -0.1299 1.5447 -0.1069 -0.4303 -0.1201 -0.1348 -1.4063 0.0405 0.0950 -0.3861 -0.3419 0.1636 -0.0916 0.4773 1.4009 0.1129 -0.1784 2.3668 -0.6329 -1.3992 -0.3356 -2.7941 0.0486 -1.4069 -0.2913 ans(:,:,7) = 1.0e+03 * -0.9246 0.9247 -0.2516 0.8610 -0.1088 -1.9745 -0.0041 -0.1631 -0.0106 -0.1367 -4.6723 -0.3470 -0.4910 0.1147 0.3230 -0.1174 0.4297 -0.0122 -3.2612 0.6263 0.0369 -0.0668 -0.0344 0.0277 -0.1161 0.1633 -0.0345 0.9539 0.0447 -0.0001 0.5696 1.8187 -0.1721 0.8973 -1.1114 -0.0157 1.8279 0.0004 1.5660 4.0119 0.0310 2.2729 -0.5968 -0.1525 -0.2009 -0.0017 0.0039 -0.9805 -2.5457 -0.0002 -0.0132 -0.2521 0.4434 -0.2282 0.2856 -0.0981 0.5368 -0.0057 0.0865 0.4057 0.1692 -0.1323 0.0003 -0.6710 -0.3455 0.0574 0.7690 0.2166 0.6595 1.3805 -0.1524 -0.0372 0.4378 -0.1823 -0.7529 0.0715 1.5798 0.1488 0.0752 0.0852 0.8656 -3.0872 0.2771 -0.0012 0.0023 -0.1716 0.0979 0.0903 1.8265 0.0630 -0.0339 -0.3900 0.9840 1.0036 0.2188 0.5097 -0.0374 -0.0500 1.0386 0.2661 ans(:,:,8) = 1.0e+03 * 0.1208 0.9851 0.2309 0.8006 0.0909 -1.5822 -0.0211 -0.1055 -0.0088 -0.3732 4.2202 -0.0464 -1.1752 -0.2563 0.1318 0.2347 0.1790 0.5631 -1.9698 0.6568 -0.0113 0.0693 0.0147 0.0020 0.3543 -0.0480 0.0170 1.0175 -0.0169 0.1656 -0.0901 3.8396 4.0411 0.1892 0.4718 -0.3003 3.3985 0.0007 1.0215 1.1185 -0.0924 -1.4184 0.0918 -0.3587 -0.4655 0.1960 0.0089 1.7213 2.7021 -0.0003 -0.0074 0.0187 0.2627 -0.0623 -0.1389 -0.0708 -0.5450 0.0082 0.2206 -1.4634 0.1109 -0.0169 0.0003 0.2150 0.0619 0.0037 1.2796 0.4563 -0.5673 0.9885 -0.6387 -0.2993 0.0804 -0.7737 2.0979 0.1963 1.6708 0.2315 0.0515 0.1988 2.4955 -1.3058 0.0010 0.0018 -0.0250 0.0508 0.2221 -0.0937 -1.3339 -0.0470 -0.0323 -0.4295 0.0009 -0.3641 0.1326 -0.2094 0.0203 -0.3311 -0.5677 0.3180 ans(:,:,9) = 1.0e+03 * 0.2391 0.2202 0.2518 1.1186 -0.4326 -0.4994 0.0630 4.4308 0.0698 2.6025 -3.2225 0.0305 -2.9660 0.2532 0.1668 -0.1236 0.2387 1.0608 2.6599 -0.8925 -0.0113 -0.1967 0.0219 0.0000 -1.0393 -0.0151 -0.0198 1.0936 0.0666 -0.2983 0.4531 -5.7338 1.5350 0.2758 -0.0070 0.9083 3.3170 0.0003 -1.1069 0.5976 0.2787 0.0555 0.0496 -0.5278 1.0354 -0.6164 0.0614 -0.4745 -1.9333 -0.0003 -0.0384 -0.0561 -0.3176 -0.1744 1.1045 -0.0477 -1.8928 -0.0013 0.0652 0.9174 0.0393 -0.0140 -0.0003 -0.2317 -0.0133 0.0175 0.1511 0.3465 -0.5838 0.8099 -0.5407 -0.0260 0.1575 1.0045 1.3247 -0.0277 -1.6021 -2.1056 -0.1351 3.0227 -2.9837 1.3324 0.0058 -0.0054 0.0128 -0.0456 -0.6410 -0.0418 -1.2990 -0.0327 -0.0824 0.5096 -0.0010 -0.4729 -0.0104 0.0522 0.0301 0.0559 0.6431 -0.0802 ans(:,:,10) = 1.0e+03 * 0.0101 -0.0651 -0.6924 0.6516 -0.0947 0.4295 -0.0260 2.9616 1.2680 -2.6672 3.6790 -0.0177 1.8374 0.1970 -0.1947 0.0518 0.0372 0.6492 -1.2272 0.7372 0.1601 0.1107 0.0265 0.0000 0.8904 0.0359 0.9613 -1.3734 -0.0880 1.0038 0.0672 -4.8124 1.5245 -0.5497 0.0120 0.9585 -2.9503 -0.0010 -2.8443 -0.0639 0.5953 -0.0899 -0.0610 -0.1771 -0.6420 -0.9580 1.1569 0.7446 1.2657 0.0028 0.0310 -0.0615 0.7809 0.1181 1.7646 -0.4244 -1.8982 -0.0027 0.0167 -0.8705 -0.0024 -0.1502 -0.0155 ...

Not enough input arguments. Error in moveprod (line 2) movprod(A,k,dim) Error in Test6 (line 8) C(m,n,:) = moveprod(squeeze(A(m,n,:)),k);

Suggested Problems

More from this Author30

Community Treasure Hunt

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

Start Hunting!