Cody

Problem 42798. Moving Product (Easy)

Solution 1446295

Submitted on 21 Feb 2018
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))

Index exceeds matrix dimensions. Error in moveprod (line 5) dim = varargin{1}; 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)))

Index exceeds matrix dimensions. Error in moveprod (line 5) dim = varargin{1}; 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))

Index exceeds matrix dimensions. Error in moveprod (line 5) dim = varargin{1}; 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)))

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

Index exceeds matrix dimensions. Error in moveprod (line 5) dim = varargin{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)))

Index exceeds matrix dimensions. Error in moveprod (line 5) dim = varargin{1}; 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!