From: Michael Orlitzky Date: Sun, 3 Mar 2013 23:13:24 +0000 (-0500) Subject: Add construct() and its tests. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=b619f0c2d08f80974d7a63b8d7fb742799949c4b;p=octave.git Add construct() and its tests. --- diff --git a/construct.m b/construct.m new file mode 100644 index 0000000..61f0caa --- /dev/null +++ b/construct.m @@ -0,0 +1,20 @@ +function A = construct(m, n, f) + % + % Construct an m-by-n matrix with entries a_ij = f(i,j). + % + if ((m < 0) || (n < 0)) + A = NA; + return; + elseif ((m == 0) || (n == 0)) + A = NA; + return; + end + + A = zeros(m,n); + + for i = [ 1 : m ] + for j = [ 1 : n ] + A(i,j) = f(i,j); + end + end +end diff --git a/tests/construct_tests.m b/tests/construct_tests.m new file mode 100644 index 0000000..cb27d1a --- /dev/null +++ b/tests/construct_tests.m @@ -0,0 +1,9 @@ +A = construct(3,4, @(i,j) i+j); + +expected = [ 2, 3, 4, 5; + 3, 4, 5, 6; + 4, 5, 6, 7]; + +unit_test_equals("construct a_ij == i+j", ... + expected, ... + A);