From b619f0c2d08f80974d7a63b8d7fb742799949c4b Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 3 Mar 2013 18:13:24 -0500 Subject: [PATCH] Add construct() and its tests. --- construct.m | 20 ++++++++++++++++++++ tests/construct_tests.m | 9 +++++++++ 2 files changed, 29 insertions(+) create mode 100644 construct.m create mode 100644 tests/construct_tests.m 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); -- 2.43.2