Cody

Problem 42798. Moving Product (Easy)

Solution 3047254

Submitted on 3 Oct 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   Pass
A = [1 2 3 4 5 6]; C = [6 24 60 120]; assert(isequal(moveprod(A,3),C))

B = 6 24 60 120

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

B = 6×0 empty double matrix

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

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

Index in position 2 exceeds array bounds (must not exceed 5). Error in moveprod (line 6) B(p,q)=(A(p,q)*A(p,q+1)*A(p,q+2)); Error in Test3 (line 6) assert(isequal(moveprod(A,2),C))

4   Pass
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.3870 2.3221 1.1611 0 0 0.1935 1.1611 1.1611 -0.9675

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 42 -189 -63 -54 30 120 -140 0 0 0 441 -630 280 320 -360 -720 -720 -144 -54 -30 15 15 -18 -126 42 -42 -18 -108 -108 96 -192 -256 -288 324 -648 432 -432 216 -216 -120 -300 200 -252 252 126 54 27 -45 -15 15 -15 -150 -400 -320 -288 72 0 0 900 0 0 0 -270 -270 378 168 84 -24 -24 16 -24 192 32 -48 6 -12 -14 0 0 0 -648 504 280 140 -14 -70 -105 -150 -90 -270 54 72 -24 0 0 0 63 0 135 135 -60 160 0 0 0 0 0 0 48 120 -120 -480 -100 -140 -28 14 -2 6 12 -60 -40 20 12 0 0 0 0 0 0 400 -320 240 -432 540 -360 160 80 80 160 256 Columns 15 through 28 320 560 -350 0 0 0 0 0 240 -120 -150 200 -200 -360 144 288 0 0 0 64 16 -2 4 10 -10 20 20 -120 80 32 40 160 120 -120 30 -10 2 8 -80 400 -450 -405 -162 90 -225 75 105 84 28 -8 20 -40 -40 28 42 -21 -16 0 0 0 24 -27 81 -135 -135 405 -648 -360 40 0 120 240 -180 -150 -90 -150 -270 270 540 -180 60 42 14 7 0 0 320 360 216 -216 -24 0 0 0 15 -27 9 -18 384 -128 112 0 0 0 -350 105 -105 -147 -147 84 -36 -108 -8 -6 54 0 0 0 -720 504 504 441 126 126 -108 -378 -640 160 -60 -18 45 -135 0 0 0 70 30 18 -81 -54 Columns 29 through 42 -360 -432 384 0 0 0 -168 -168 56 63 -72 432 -144 -36 60 48 -64 -224 168 42 -36 108 270 -315 210 84 -36 -30 405 270 -210 -420 -210 210 189 315 -225 75 105 -105 315 -90 18 -6 -18 6 24 0 0 0 270 30 36 -18 -36 18 0 0 280 35 56 -32 224 -224 -56 -80 30 90 9 3 -8 -48 432 432 648 -648 324 -324 72 162 -180 540 120 -12 6 -42 -168 -224 -224 280 490 210 189 -135 135 -15 -12 20 -81 108 60 -140 315 567 -81 -63 63 -252 -180 -180 225 225 126 -84 -96 256 256 -224 168 126 72 -12 -12 6 42 42 144 16 40 40 360 -720 -180 40 -40 100 -100 -50 -80 360 Columns 43 through 56 -42 14 70 -100 -500 150 -90 18 36 36 180 210 -350 -420 -120 -80 112 -126 -126 -72 -32 -16 0 0 0 -150 -75 120 36 20 -50 -100 20 -16 4 36 63 0 0 0 800 -640 42 0 0 0 -216 36 60 90 360 144 -16 -28 -28 -168 0 0 0 720 0 0 0 -448 224 -140 180 90 -144 144 -10 -20 -60 96 0 0 0 50 60 0 0 0 -144 -144 0 0 0 -140 -28 28 140 -700 800 720 720 270 180 -162 -100 -20 -36 -27 189 168 -168 -240 -150 0 0 0 -216 -378 0 0 0 -90 720 -640 448 336 -252 180 0 0 0 -35 -720 -900 800 0 0 0 -160 400 -200 -180 -36 -18 12 -48 Columns 57 through 70 -300 150 175 -175 210 210 252 126 -144 168 -112 -56 -24 -48 144 -336 378 315 270 -90 18 -27 54 108 72 96 96 -96 256 96 108 189 -252 168 -168 -168 224 32 -80 -90 450 -270 -24 -54 -90 810 90 -45 50 -250 -150 -150 -120 40 40 -100 -72 54 42 420 -210 30 9 -30 270 270 -162 -54 0 0 -192 -48 -32 20 180 -45 -36 0 0 0 -120 240 -240 -180 0 0 0 490 -98 -56 -16 -80 120 300 0 0 0 -2 315 -210 -90 18 18 -48 432 -576 -576 128 0 0 0 -90 105 -84 -24 56 56 -84 0 0 0 224 288 -648 729 162 -168 -196 490 0 0 0 240 300 420 -560 -56 -8 -6 30 Columns 71 through 84 -12 32 -40 -160 0 0 0 -30 270 -720 -72 48 54 486 36 -126 -42 -98 -70 245 315 -567 -486 216 120 180 -135 27 240 -240 160 -100 -100 -250 -450 0 0 0 320 560 224 252 1000 300 -120 24 -40 100 -400 720 504 -63 21 27 27 72 0 -27 63 21 -28 -12 -96 168 -224 252 -108 162 -90 -30 72 120 -320 -640 -192 -96 24 80 -60 270 162 -378 0 0 18 -90 270 30 -30 -60 600 60 30 3 6 -8 -32 -32 144 64 -256 320 800 -800 80 8 1 3 27 108 -324 252 -54 12 24 ...

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 100-by-0. 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 = Columns 1 through 17 -31.0101 57.0570 -74.1707 -70.4624 203.0797 579.4240 -450.2518 -438.3942 421.9528 -288.0716 330.4148 -323.5989 -684.9911 -335.6305 347.5712 364.8932 859.9603 251.4721 -472.4202 -674.6276 574.9751 -395.5877 139.6930 101.0007 -17.4521 20.0860 20.3511 -185.0145 179.4821 267.5976 256.5846 285.5858 66.9711 18.1441 -466.9241 -472.4659 -465.6753 358.7363 -292.5618 14.2526 13.6057 -1.0039 20.3490 -2.8870 9.3512 -12.3276 52.4005 16.5347 -12.7000 26.5311 399.5087 34.1257 32.4529 -17.8562 -284.2222 -50.9479 -96.3763 81.4879 -77.9205 59.7405 -10.5396 56.4102 45.4894 243.4478 176.8963 -200.1043 16.6106 -11.1894 -296.0980 379.1068 83.9195 -56.9659 9.9848 5.8085 -0.9387 -3.0866 -2.0404 2.0770 1.6153 16.7365 -3.7599 -0.0874 0.0199 0.7297 -28.3771 -248.5993 200.0496 -43.0079 45.3012 -54.9911 -213.8272 142.9028 -42.7678 42.5194 47.4976 -103.1737 81.6546 -128.3858 -132.4840 -179.3828 -130.5004 -298.7255 -6.8772 -77.4911 -497.9513 -482.7800 -41.5361 42.3929 -41.7292 112.7108 -62.9500 23.3329 -7.4344 -1.6121 -1.9401 -12.7807 49.8047 -21.9708 50.1924 -19.1551 -19.1554 -10.7346 200.2645 359.8084 462.9228 448.7881 4.1209 0.4333 0.2908 26.4430 -3.4476 1.8469 0.5302 -55.0056 -69.4381 138.8002 1.6475 1.9825 -23.2786 -342.0803 249.8106 240.5097 130.6218 207.0333 -138.4194 -25.5401 7.4690 8.5802 91.7041 -192.1113 208.6822 38.7153 -57.2537 49.5236 249.5736 420.3621 -131.5448 -78.8707 61.4431 212.2291 -385.0754 -841.5754 -560.2154 259.4540 185.9178 -132.2126 143.1706 101.5584 -218.8688 116.9360 Columns 18 through 34 -205.1190 141.8276 98.8697 -278.2807 329.2163 -301.2931 -331.4198 12.5274 18.6375 -13.8571 -38.5538 -39.1819 25.6257 11.9985 -2.2613 -4.7616 19.4039 -20.1901 -13.8428 57.0028 41.8408 220.5351 120.6842 43.6318 56.8732 -16.1155 -93.2184 -27.3096 -17.1517 6.5240 -15.5568 137.5139 -388.2660 -268.7292 -369.3779 -119.9326 -79.8936 -33.4569 90.1133 -103.5724 231.6699 38.6436 29.3943 -9.0850 -36.3043 83.8051 268.0393 -19.4085 11.8799 5.1490 90.3072 14.7717 -132.8831 -120.9110 80.2774 -91.6062 -173.1107 -34.9314 -47.3360 -62.5293 -748.1309 -828.8857 760.3574 -624.5113 -436.1379 -486.4297 552.6186 -488.3920 -79.9265 3.4996 0.3763 -0.4430 12.1867 264.8736 -270.7002 -244.3376 -29.0489 -23.6274 -1.5409 7.2957 8.4617 -66.9569 113.3955 15.8977 33.1285 150.4305 54.7643 49.6453 -20.9644 16.8039 21.5664 26.0080 99.1433 -30.9190 19.0811 7.7217 10.6062 77.0956 103.4838 232.3442 -280.5162 -439.1771 -36.4603 196.9655 216.0337 -31.7023 27.9061 3.7658 22.8786 19.5352 102.4020 17.3072 -24.5352 -25.8732 310.5376 -343.2315 58.5610 7.0073 -6.3025 125.0918 -259.4387 -355.1602 132.9419 -165.0394 -199.4593 145.9166 -45.2143 63.5796 71.1685 193.6974 128.2898 19.8159 4.5947 -7.2168 -152.1982 590.9692 63.9494 356.0139 510.8335 380.8172 189.6059 -103.8516 -35.7437 -10.7434 17.6166 2.9878 -26.4685 -20.4537 42.8632 -34.4352 44.0802 -217.8474 -164.4322 180.8179 60.8913 11.8189 -9.6363 24.1093 345.3369 -373.0719 206.6385 -268.0359 -124.0706 79.3402 73.3175 80.0937 222.1181 174.4676 78.6863 -46.3043 Columns 35 through 51 49.5536 25.4181 131.6242 -187.0853 -251.6622 71.6606 -72.2854 42.1519 122.1755 93.2608 -88.9582 26.6099 46.3664 -121.9173 331.7249 57.3979 54.2787 154.4211 11.3552 6.5140 -0.6860 -9.2342 20.7625 -102.8907 70.6389 49.2489 -127.3415 61.0449 102.6409 -157.5127 135.7497 16.9227 16.7575 24.6191 92.3722 71.5245 70.5420 0.1660 0.6376 0.4651 -193.5502 -82.0439 38.8450 -58.2643 -164.8154 -133.3228 -140.2938 133.3330 -280.7889 -33.1599 -16.0445 464.3051 -375.8992 -222.6040 243.1682 -163.1034 -260.7217 -40.4074 76.1660 -73.3248 122.7291 40.7975 41.7544 16.0219 21.2355 6.3539 -38.9633 -74.1730 -45.4031 475.1740 -425.3692 362.2463 -162.1585 22.3930 13.3381 -30.7671 124.7176 -242.8153 122.8324 308.4682 19.9299 13.4375 14.2874 69.6651 -56.9738 -267.0219 -148.9801 -20.7104 -45.6675 -78.4217 226.6023 -201.3585 -132.5547 -244.1904 -206.5961 133.0684 -32.2683 -8.0110 2.6465 -15.2185 -40.6131 58.1511 46.2986 -499.5716 -524.1402 54.2577 60.3471 -68.2635 -251.8830 80.6925 30.5512 -35.1857 11.8976 -0.0116 0.0257 0.0497 27.4904 20.8971 7.5520 -550.7963 -263.9926 -33.4478 33.9060 68.4110 484.4943 40.0761 6.9681 6.1170 -47.4851 -230.9493 170.2363 -69.9529 -64.1337 29.1408 -105.9464 -9.3179 -162.3290 256.7292 433.1516 -99.0644 -96.3433 -73.9444 -40.2567 -64.7903 -16.8875 75.3055 -45.8558 -148.7183 164.5272 127.3877 -146.8673 -283.2961 640.8895 -4.1078 6.6131 12.5863 -105.1302 -56.2585 -32.1059 36.2245 190.6486 136.9105 -180.4154 86.3147 225.8364 -179.7391 -522.2694 326.0274 -146.1017 5.6339 Columns 52 through 68 53.1732 315.9057 -367.7620 329.9777 290.3907 103.4745 -82.0062 -99.4344 -174.7601 -219.2494 -195.3291 159.5663 -285.6688 194.4516 426.4298 77.0652 89.3603 248.9433 -226.3427 756.7223 19.2087 -6.5613 -1.8543 39.9344 -56.9117 -125.1493 77.1372 -115.1226 -22.4495 -64.0943 12.9117 -45.9191 15.6337 97.6434 15.8937 35.6693 56.8101 -32.9873 -118.3699 -58.9904 -107.6416 -129.2421 309.9585 276.9310 -174.7438 6.6638 -9.8597 10.8926 -258.9857 -219.3667 -8.6101 431.4529 -657.6195 379.4936 338.9238 -164.9249 -182.5952 -32.0531 -62.5629 -15.2702 -29.7339 -20.6632 160.2799 142.8582 -46.7729 -5.4112 5.8295 9.8771 -41.4507 -41.5106 -87.4492 21.1326 6.7417 -11.0135 64.8698 -494.6953 208.9101 76.1639 -70.8613 -51.6305 42.3623 -28.1980 -96.9124 39.8560 -96.8973 71.0304 -130.6261 63.0497 -2.8929 0.5727 -4.1199 -25.8945 -107.0162 -33.1309 134.8513 -10.7647 40.5134 6.4641 -13.2251 11.2337 29.2077 -33.1726 -16.3241 3.2475 37.1907 -15.6377 -35.6707 40.1546 -239.2604 290.4784 345.0501 -318.8163 648.1102 77.5245 -55.1023 -58.7452 -292.3925 294.7631 369.7757 16.5922 2.9782 47.9748 134.0261 322.5982 102.7469 -47.3952 -1.1500 -1.0332 2.1444 66.7428 -207.7948 58.8153 -83.5783 -36.0479 -7.7931 1.3495 -281.6335 -175.0685 172.4985 15.9524 -1.5352 -0.8166 17.4756 189.8803 -33.9764 32.5860 -26.4420 -210.8552 -61.1925 42.7334 25.3183 124.4394 192.6771 7.3279 -16.5872 89.0030 -21.6112 -3.0407 0.8316 -0.3236 -1.5685 23.0536 -138.8046 261.2298 -119.1717 122.7209 -61.0294 143.9208 101.3273 66.9164 Columns 69 through 85 -16.0157 -21.2727 33.2579 -12.3237 36.7601 -20.5015 98.2274 -150.7467 112.2166 -363.7342 -214.4969 69.4432 27.1882 -3.6502 -11.9025 3.3673 -26.0834 -216.1256 -694.4954 -651.7971 41.7504 4.7398 4.5514 20.4523 129.1959 -10.9465 5.9901 4.3226 17.6999 49.2380 -32.2804 -1.0625 -1.3473 3.1470 -10.3465 -6.3758 -6.5737 -2.7095 4.9627 26.4833 -60.9507 -38.2536 -156.3937 73.8425 42.6079 -1.5763 -0.1729 -0.1557 5.1291 92.5170 -96.6749 -71.5795 -82.3220 63.5473 72.9064 183.6919 433.8633 -455.8074 -308.6872 -331.1496 -341.7390 -456.3165 223.8996 -99.1164 -97.3273 74.4997 109.9676 38.5157 98.3975 184.6406 -91.4016 12.0196 -11.7049 1.9995 -5.9921 -26.8006 185.9426 -312.2469 -243.2596 198.6690 -290.2235 148.5540 154.8437 -136.3358 -159.2739 -11.3893 -12.0532 56.5635 240.6348 -546.5579 577.9256 401.6123 -119.5058 125.6385 73.3074 -132.5721 -185.2099 415.4724 640.2510 262.3874 -108.2400 46.8023 -224.8994 -335.2994 -265.8416 -306.3228 305.0074 -204.0733 222.7658 51.0473 -21.0591 -29.8106 -57.4273 -192.2879 128.5138 -66.3394 87.9215 -179.1353 -480.6903 -2.8557 33.4043 151.2414 55.6908 -49.6168 -97.1257 209.7245 209.1806 -17.4763 -22.4203 -10.1687 -83.7763 -62.3343 -166.3605 163.6344 130.0964 178.3174 189.0708 -145.3837 -57.6914 -75.7795 50.7731 65.9808 -45.9582 82.3855 105.2799 -213.3928 134.5847 54.2217 5.4196 9.7985 -23.5476 -175.8236 -183.8194 37.9838 47.3104 8.6067 7.4748 14.7611 -221.3855 -523.8204 270.8929 256.0402 -95.0057 139.1127 -184.0294 83.7964 -96.1046 -78.2962 -134.8388 -118.8693 Columns 86 through 102 -7.0921 -52.0604 -23.7822 -193.8252 59.5364 -85.2830 -40.5528 -5.9739 -14.0092 -25.3490 -172.0008 -118.5102 56.9286 142.8799 188.1181 291.5053 478.3340 -53.1927 -31.2688 -41.0267 -229.2600 -590.7281 -317.3409 -242.0214 -121.2161 -166.2358 -96.2724 -86.8878 75.5709 -46.9229 90.0108 -48.1348 -12.3669 -3.6250 14.2739 17.8950 -19.4719 -178.9735 -8.8429 0.0981 0.0470 0.4848 -93.7689 -5.5062 4.7833 -4.2719 -136.6698 -230.2289 -140.9308 138.4159 -164.7658 -118.8753 6.5271 -3.4712 3.6716 -1.7432 1.2281 0.3051 9.1017 25.6734 5.9834 -11.7653 0.3083 -6.2905 4.8363 227.9302 177.6908 -217.9157 248.0418 -299.4820 210.5996 -18.2749 9.9308 -4.2043 -43.5606 -40.6430 -129.5395 182.8979 147.9012 115.2157 26.4398 6.9704 2.9645 -11.8490 109.1463 90.0585 -50.3956 13.3030 -0.6227 2.7944 23.9870 -74.4192 -9.1620 -3.3467 -23.3444 -160.6399 -455.9699 -69.9702 93.1803 -58.3678 -243.8340 63.3197 14.5427 -15.8248 -1.0212 27.3659 -20.4461 32.8235 -6.1503 -7.2674 -69.0000 211.9501 152.5928 -245.0631 271.2235 266.3932 266.0899 -248.4108 352.6302 199.5980 -164.8051 -105.3181 42.5519 64.1411 45.5160 129.3333 -70.4257 -37.6885 -...

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 100-by-0. 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!