Cody

# Problem 42798. Moving Product (Easy)

Solution 2195619

Submitted on 6 Apr 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))

B = 6 24 60 120 30

Assertion failed.

2   Pass
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)))

B = 0.0556 0.1667 0.3333 0.5556 0.8333

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))

B = -2 6 4 0 0 3 3 -2 6 12 6 -2 6 -15

Assertion failed.

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)))

B = -0.1935 0.6079 0 0 0 0.3040 -0.1935 -0.2026 1.2159 1.1611 0.6079 0.6079 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))

B = Columns 1 through 14 0 0 0 -504 504 588 -294 42 -42 -10 30 210 -1260 4410 Columns 15 through 28 -2646 -1764 -756 126 18 200 1800 9000 -20250 2025 -3645 -2835 3402 378 Columns 29 through 42 -378 -35 -70 -70 -80 -720 144 360 -360 45 5 80 480 4800 Columns 43 through 56 3360 20160 -15120 20160 -6048 -864 -144 -64 640 5760 3600 4050 40500 -24300 Columns 57 through 70 -27000 -5400 -600 -210 630 -1260 1440 -2160 -432 720 -1080 135 15 -63 Columns 71 through 84 -441 -1764 -392 -392 -392 112 140 -70 70 80 800 800 2000 -3600 Columns 85 through 98 1350 405 4050 810 90 -120 -360 0 0 0 0 0 0 0 Columns 99 through 112 -14 -48 288 2880 8640 20160 -7560 -2520 2268 378 54 0 0 0 Columns 113 through 126 560 -140 560 -224 112 16 -16 20 60 300 150 -750 375 -1000 Columns 127 through 140 1800 1800 360 0 0 0 0 0 0 0 3360 1680 -420 0 Columns 141 through 154 0 0 -48 60 420 315 -105 105 -21 28 252 2268 -11340 -2835 Columns 155 through 168 -810 900 -900 180 -180 -350 -700 -3500 4200 1680 -1680 -3360 4032 -672 Columns 169 through 182 168 -350 3500 3500 -1500 450 450 45 -180 -60 -20 0 0 0 Columns 183 through 196 0 0 0 0 0 245 245 -60 240 -480 96 -144 216 -216 Columns 197 through 210 -756 -756 252 480 -2400 24000 9000 1500 600 -480 144 48 -48 -6 Columns 211 through 224 24 -144 144 576 -1536 3456 -5760 -2880 -720 -560 -5600 -44800 -16800 -12000 Columns 225 through 238 -9600 -6720 -840 -280 -56 36 -324 -1620 810 405 4050 2700 4860 -1620 Columns 239 through 252 -540 -192 -576 4032 0 0 0 0 0 56 8 -8 32 128 Columns 253 through 266 32 -64 512 1152 2304 -1152 576 20 -120 -120 36 288 -720 1080 Columns 267 through 280 5400 -1800 225 0 0 0 0 0 -1024 1536 -1152 -1152 -288 40 Columns 281 through 294 -160 320 2240 2800 0 0 0 0 0 0 0 0 0 -2400 Columns 295 through 308 -2400 -4800 1920 480 -80 80 -640 -640 160 -120 -144 -108 -108 108 Columns 309 through 322 36 160 -1440 12960 0 0 0 0 0 0 -50 250 1500 4500 Columns 323 through 336 9000 -6300 -10080 8400 -16800 -1680 240 16 -64 128 160 -800 -2400 -2400 Columns 337 through 350 0 0 0 0 0 0 0 0 -486 270 90 -30 -30 200 Columns 351 through 364 1000 10000 -20000 -25000 -50000 -20000 18000 -1800 180 -240 -1680 -16800 3360 -3360 Columns 365 through 378 4480 -1920 -576 -576 -72 -160 480 3840 -15360 5760 5184 -15552 7776 -972 Columns 379 through 392 -324 0 0 0 0 -7560 -5040 8400 5600 800 160 480 -2400 19200 Columns 393 through 406 0 0 0 0 0 280 -40 200 -1000 4000 -2000 2500 -3500 -2100 Columns 407 through 420 4200 840 168 576 -5184 -25920 5760 -5760 -3600 3600 -2160 1080 135 -240 Columns 421 through 434 -1200 7200 -24000 15000 -9000 12600 21000 -2100 -420 225 -900 900 180 120 Columns 435 through 448 120 240 2400 -2400 -400 0 0 0 -13720 15680 -23520 0 0 0 Columns 449 through 462 0 -576 576 2880 720 -630 630 -3780 1512 756 108 -36 324 2592 Columns 463 through 476 648 -648 72 -32 -8 -8 8 56 168 504 -1008 -1152 -3456 11520 Columns 477 through 490 -11520 -1440 180 0 0 0 0 0 0 0 0 0 0 -12 Columns 491 through 504 120 240 -600 -600 200 -180 270 -27 27 400 0 0 0 0 Columns 505 through 518 0 0 0 0 0 0 0 0 2800 3200 640 -512 2048 512 Columns 519 through 532 -64 -350 700 7000 -1400 -1800 1440 -3600 -1080 -1080 -120 -12 84 84 Columns 533 through 546 378 1890 -1260 -1440 10080 -1120 112 168 -840 -3360 -1680 -2240 -2240 3584 Columns 547 through 560 -5376 -1344 336 240 2160 -6480 -4860 5670 5670 4410 5880 -1960 280 450 Columns 561 through 574 3150 3150 2800 5040 5040 -7200 14400 1800 -200 512 -2560 2560 0 0 Columns 575 through 588 0 0 0 320 32 7 70 700 4900 -34300 -4900 -2940 -2646 -378 Columns 589 through 602 -54 20 -40 -80 -720 432 432 -1080 -2160 240 40 60 -120 240 Columns 603 through 616 720 144 -36 0 0 0 0 -100 600 4800 1440 0 0 0 Columns 617 through 630 0 0 18 -294 -2646 23814 -39690 -56700 -24300 -27000 -9000 -900 -90 -196 Columns 631 through 644 392 -1960 -2240 3920 1120 560 112 14 -2 -15 135 -405 -3645 -8505 Columns 645 through 658 -10206 -2268 3024 -336 48 -240 -1920 -19200 19200 19200 -32000 -28000 -2800 -350 Columns 659 through 672 70 80 -480 -1920 192 480 960 -960 1680 -1680 168 84 504 -2016 Columns 673 through 686 -672 -672 96 -48 96 96 24 0 0 0 -1512 3024 2016 3360 Columns 687 through 700 0 0 0 -15 150 -300 600 -3600 5040 1008 5040 840 140 40 Columns 701 through 714 -120 -480 384 1152 -1008 -3024 3780 -945 315 40 200 ...

Unable to perform assignment because the size of the left side is 1-by-1-by-96 and the size of the right side is 99-by-1. 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)))

B = 1.0e+03 * Columns 1 through 17 0.0124 -0.0195 0.5342 -0.6446 -0.6683 2.6842 -1.2650 -0.1170 0.0193 -0.0026 -0.0230 -0.1227 0.1744 -1.7906 0.8942 1.2795 2.3764 Columns 18 through 34 1.0265 -0.2314 -0.0026 0.0040 -0.0179 0.0003 -0.0012 0.0000 -0.0001 -0.0263 0.0107 0.2160 -0.0040 -0.0266 0.2662 0.2015 0.1132 Columns 35 through 51 0.0756 0.0617 -0.1049 -2.1757 -0.4887 0.0546 0.1452 0.7649 0.1511 0.0135 -0.0289 -0.0102 -0.0265 0.4832 -0.0850 0.0053 0.0477 Columns 52 through 68 -0.1468 0.2616 -0.8658 -0.6858 -1.9608 2.8579 -1.8207 -0.2530 -0.0024 -0.0004 -0.0009 -0.0112 0.0153 -0.5314 -1.5057 -0.3881 -0.0992 Columns 69 through 85 0.0164 -0.0000 -0.0003 -0.0001 0.0287 -0.0213 -0.0306 0.1385 0.0359 -0.0291 -0.0035 0.0151 -0.1112 -0.5716 0.6706 -1.8289 2.4152 Columns 86 through 102 -0.1961 0.0220 -0.0010 -0.0001 -0.0669 -0.0458 0.1418 0.1055 0.0643 -0.5278 1.1750 -1.7018 0.5918 -0.1054 -0.0489 -0.1510 0.5705 Columns 103 through 119 0.5669 -0.1634 -0.2123 0.1400 0.0958 -0.2252 -0.0561 0.0097 -0.0483 -0.4645 -1.0217 -0.9988 -0.2138 0.0443 0.0677 -0.0728 0.0685 Columns 120 through 136 0.0079 -0.0084 0.0496 -0.0667 -0.0047 -0.0038 -0.0013 0.0038 0.0498 -0.0586 -0.0120 0.0332 0.2726 0.1326 -0.2474 -0.8075 0.5480 Columns 137 through 153 0.9099 0.0699 -0.0077 0.0006 0.0039 0.0043 0.0138 0.0161 0.0089 0.0626 0.0326 -0.0503 -0.0134 0.0224 -0.1333 -0.1870 -0.0838 Columns 154 through 170 -0.1023 -0.1374 -0.7553 0.4591 -0.3556 0.0444 0.0298 0.0059 -0.0336 -0.0147 -0.0245 0.3622 -0.5741 2.2911 -1.5172 0.5178 0.0376 Columns 171 through 187 0.2204 -1.1621 0.0805 -0.0428 0.0421 -0.0764 0.2704 0.5179 -0.0899 0.0014 -0.0022 0.0089 0.1071 -0.0816 -0.0305 0.0101 0.0159 Columns 188 through 204 0.0206 -0.0355 -0.0093 -0.0882 -0.6372 1.3657 3.0956 1.3317 -1.2360 0.5879 -0.4785 -0.1175 -0.0356 0.1163 0.8168 0.2244 -0.0546 Columns 205 through 221 0.1079 0.0454 0.0469 -0.3574 -0.0554 0.0221 -0.1700 -1.1299 0.3476 -0.6594 -0.7085 0.8104 1.9595 -0.5840 0.0708 -0.0564 -0.5376 Columns 222 through 238 4.4348 -5.4319 3.4954 -1.1015 -0.1436 0.0411 -0.0145 0.0048 -0.0874 0.4000 -3.3357 2.6964 -0.3300 0.3620 0.0823 -0.0198 0.1329 Columns 239 through 255 0.0265 0.0455 -0.3141 -3.1176 1.6529 -0.6078 0.5438 0.4333 -0.5455 -0.7754 -0.1256 -0.0069 -0.0302 0.2812 0.1779 -0.1482 -0.1242 Columns 256 through 272 -0.0142 0.0379 0.0115 0.0031 0.0006 -0.0011 -0.0016 -0.0228 0.0581 0.0964 -0.4740 0.3629 1.3816 -0.4128 -0.0573 -0.3518 -2.2769 Columns 273 through 289 2.4353 -0.3405 -0.2175 -0.0825 -0.1040 0.7523 0.1919 -0.0047 0.0013 -0.0086 0.0023 -0.0199 0.2334 -0.0711 -0.1188 0.0105 0.0033 Columns 290 through 306 0.0465 -0.1968 0.1091 -0.0108 0.0152 -0.0084 0.1306 0.9564 -0.8481 -0.3647 -0.0132 -0.0752 -0.2893 0.2687 -0.2526 -0.1288 0.2508 Columns 307 through 323 0.2566 -1.2017 -0.4120 -0.0169 -0.0191 0.1620 0.1376 0.0549 -0.0318 -0.0141 -0.0159 -0.0556 0.0845 -0.0003 -0.0020 0.0132 0.0848 Columns 324 through 340 -2.5433 1.4669 -1.9645 -0.4484 0.6526 -0.1420 0.0307 -0.2793 1.1823 -1.5639 0.7445 0.1911 -0.1693 -0.0032 -0.0074 0.0032 -0.0778 Columns 341 through 357 -0.5350 5.1475 3.0551 -3.1473 0.3825 -0.3824 0.6743 0.0031 -0.0037 0.0091 -0.0659 0.1358 0.2276 -0.4069 -0.5637 -0.7357 -0.1817 Columns 358 through 374 -0.1434 0.0143 0.0044 -0.0090 -0.0619 -0.0622 -0.6399 1.4757 0.9474 0.2174 -0.0944 -0.0199 0.0113 0.0144 -0.0889 -0.0416 -0.0501 Columns 375 through 391 0.1380 -0.1771 0.9414 0.4669 -0.1327 0.0756 0.7330 -5.0171 -2.5321 2.4777 -1.3681 1.4772 -1.2011 1.3358 -0.2496 -0.0515 -0.2775 Columns 392 through 408 2.0894 -1.3856 0.4566 0.1126 0.1352 0.0279 0.0676 0.0509 0.0261 0.2522 -1.9528 -0.8898 0.7229 0.6298 -0.7977 2.9649 2.7023 Columns 409 through 425 0.3213 -0.0129 -0.0667 0.1764 0.5016 -0.1367 -0.1465 0.2526 0.2737 -0.5335 -0.0964 -0.0215 -0.1826 -1.3358 -2.9973 2.9935 0.1922 Columns 426 through 442 -0.0853 -0.0215 -0.0132 -0.0243 -0.0226 -0.0548 -0.0444 -0.1001 -0.1062 -0.0050 0.0162 0.0033 -0.0022 -0.0193 -0.0179 -0.1630 1.3524 Columns 443 through 459 0.6828 0.1399 0.1315 -0.0004 -0.0004 0.0080 0.0009 0.0169 -0.0978 0.5099 0.6855 -0.2040 0.3094 0.2493 -0.2580 2.4060 0.2743 Columns 460 through 476 0.0203 -0.0127 -0.1154 -0.1218 0.0695 -0.5457 -0.1174 0.0338 0.0125 0.0025 0.0162 0.1116 0.3437 1.6996 1.5499 1.0767 0.6098 Columns 477 through 493 -0.2198 0.0369 0.0077 -0.0714 -0.0015 0.0024 0.0016 0.0007 -0.0841 0.4264 -0.3231 -0.1068 0.0435 -0.0445 0.1542 0.3857 0.1198 Columns 494 through 510 0.0643 0.1132 -0.3936 1.4521 3.8907 -0.6374 -0.0050 0.0304 0.2206 0.3199 1.1691 -1.7997 1.3637 -0.6710 -1.0145 -0.1077 0.0105 Columns 511 through 527 -0.0809 0.0359 -0.0017 0.0077 -0.0068 -0.0501 -0.4541 -0.2970 -0.0437 -0.0103 0.0954 0.8167 -0.2312 1.4564 -1.3709 1.1526 1.6329 Columns 528 through 544 0.1803 0.0207 -0.0353 0.1072 -1.0077 -0.5063 0.5658 0.3885 -0.3973 -0.7799 -0.2846 0.1363 0.0333 0.2968 1.0692 0.3708 0.5419 Columns 545 through 561 0.4227 0.4256 -0.7115 0.3208 0.0461 0.0448 0.0766 -0.4885 -0.1122 -0.0719 0.0964 -0.0585 0.1922 0.1341 -0.0584 -0.0050 0.0228 Columns 562 through 578 0.1916 -0.8387 -0.7893 0.7218 -0.0348 0.0017 -0.0055 -0.0013 0.0811 0.7627 7.1279 6.7840 5.6926 2.3372 -1.2316 1.0502 0.0814 Columns 579 through 595 0.0211 0.0577 0.1409 -0.6914 -0.9098 -0.4411 -1.3374 0.8978 -0.9230 -0.9492 -0.1281 -0.0077 0.0277 -0.2275 0.0230 0.0621 0.0499 Columns 596 through 612 -0.0484 -0.4070 0.3979 -0.1374 0.0603 0.1917 1.2715 0.8749 -0.2503 0.1475 -0.1182 0.1560 0.4834 -0.2577 0.0307 -0.2161 -1.2332 Columns 613 through 629 0.3661 -0.4090 0.2736 0.1536 -0.6324 -0.1301 -0.0276 -0.0197 0.0888 -0.7123 0.9536 -1.7313 -0.8904 -0.3577 -0.0396 0.0161 -0.0070 Columns 630 through 646 0.0160 -0.1057 0.8554 -0.4158 -0.1100 -0.0328 -0.0121 -0.0311 -0.1234 0.0628 0.0607 -0.3994 0.0416 -0.0510 -0.0493 -0.0561 -5.1169 Columns 647 through 663 -0.0856 0.0065 -0.0009 0.0033 -0.0267 -0.0482 0.0460 -0.2940 -0.2196 -0.3020 -0.1247 -0.3093 0.0511 0.0120 -0.0158 0.0466 0.0601 Columns 664 through 680 0.0499 0.1957 0.3821 0.5507 -0.5548 0.1070 0.0597 -0.4217 -2.5929 -1.7300 -2.0540 -1.3619 1.2265 0.1352 -0.0403 0.0086 0.0041 Columns 681 through 697 -0.0237 -0.0137 -0.0134 -0.1211 -0.0266 0.0399 -0.0178 -0.0231 0.0182 0.0670 -0.6667 -1.0619 -0.7565 0.6312 -0.3725 -0.0145 0.0180 Columns 698 through 714 0.0008 0.0001 0.0024 0.0041 0.0013 0.0012 0.0088 -0.0465 -0.4933 -0.5984 1.1844 -0.1313 -0.0452 -0.2635 1.0723 -1.4579 0.2807 Columns 715 through 731 -0.0938 0.0182 -0.0179 -0.0784 0.0402 0.0488 -0.0304 0.0977 0.0037 -0.0040 -0.0132 0.0162 -0.4650 -0.5096 0.2460 0.0054 0.0454 Columns 732 through 748 0.1398 -0.1072 -1.0460 -1.2091 -2.5596 1.3872 -0.9152 -0.0950 -0.0436 0.3912 -3.5197 7.0629 -7.3215 3.9232 3.2648 -1.6012 0.3541 Columns 749 through 765 0.0736 -0.0301 -0.1772 -1.6062 0.0653 -0.0175 -0.0203 -0.0058 -0.2035 -0.2220 -0.0326 -0.0294 0.1008 0.4879 -0.4767 0.2692 0.0102 Columns 766 through 782 -0.0199 0.0033 -0.0023 0.0176 -0.0510 0.4224 3.2312 3.2896 -2.9525 0.6598 -0.8545 -0.5671 -0.1110 -0.0600 -0.0095 -0.0196 -0.0620 Columns 783 through 799 0.1475 0.3474 -0.4987 -1.3640 2.2582 0.1771 -0.0598 -0.0013 0.0002 0.0011 0.0001 0.0016 0.0568 0.0512 -0.2928 -0.1845 0.0399 Columns 800 through 816 -0.0335 -0.3093 0.4377 -0.5376 0.4161 -0.0956 -0.2976 -0.0065 0.0035 -0.0017 -0.0078 0.0238 0.0609 0.0781 -0.2194 -0.1850 0.2839 Columns 817 through 833 -0.3062 -0.3053 0.1186 -0.0086 -0.0833 0.6412 -0.5979 -4.2644 -1.7793 -0.0101 -0.0062 -0.0058 -0.0014 0.0393 -0.1646 -0.2269 0.3060 Columns 834 through 850 -0.2423 0.4755 -0.5815 0.0138 0.0144 0.0017 -0.0199 -0.1745 0.0986 -0.1960 0.1607 -0.0261 0.0783 -0.0123 0.0012 -0.0009 -0.0024 Columns 851 through 867 0.0222 -0....

Unable to perform assignment because the size of the left side is 1-by-1-by-97 and the size of the right side is 99-by-1. Error in Test6 (line 8) C(m,n,:) = moveprod(squeeze(A(m,n,:)),k);

### Community Treasure Hunt

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

Start Hunting!