]> gitweb.michael.orlitzky.com - octave.git/commitdiff
Add construct() and its tests.
authorMichael Orlitzky <michael@orlitzky.com>
Sun, 3 Mar 2013 23:13:24 +0000 (18:13 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Sun, 3 Mar 2013 23:13:24 +0000 (18:13 -0500)
construct.m [new file with mode: 0644]
tests/construct_tests.m [new file with mode: 0644]

diff --git a/construct.m b/construct.m
new file mode 100644 (file)
index 0000000..61f0caa
--- /dev/null
@@ -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 (file)
index 0000000..cb27d1a
--- /dev/null
@@ -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);