diff libinterp/corefcn/data.cc @ 18145:a86d608c413c

Return empty matrix rather than issuing error when requested diagonal is out of range. * data.cc: Add %!tests for new behavior. * Array.cc (diag): Return 0x1 empty matrix when diagonal is out of range. * DiagArray2.cc (extract_diag): Return 0x1 empty matrix when diagonal is out of range. * Sparse.cc (diag): Return 0x1 empty matrix when diagonal is out of range.
author Marco Caliari <marco.caliari@univr.it>
date Tue, 17 Dec 2013 09:00:34 -0800
parents b560bac0fca2
children 775e7874b38d
line wrap: on
line diff
--- a/libinterp/corefcn/data.cc
+++ b/libinterp/corefcn/data.cc
@@ -1354,6 +1354,11 @@
 %!assert (diag ({1}, 2, 3), {1,[],[]; [],[],[]});
 %!assert (diag ({1,2}, 3, 4), {1,[],[],[]; [],2,[],[]; [],[],[],[]});
 
+## Test out-of-range diagonals
+%!assert (diag (ones (3,3), 4), zeros (0, 1))
+%!assert (diag (cell (3,3), 4), cell (0, 1))
+%!assert (diag (sparse (ones (3,3)), 4), sparse (zeros (0, 1)))
+
 %% Test input validation
 %!error <Invalid call to diag> diag ()
 %!error <Invalid call to diag> diag (1,2,3,4)