changeset 14567:5bb5fcffa29d stable

correctly fill cell arrays for three-arg diag function calls * data.cc (Fdiag): Special case for cell arrays so that off-diagonal elements are filled with [].
author John W. Eaton <jwe@octave.org>
date Tue, 17 Apr 2012 11:53:39 -0400
parents aa491bd9e19b
children 82449d607d20
files src/data.cc
diffstat 1 files changed, 17 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/data.cc	Mon Apr 16 15:58:38 2012 -0400
+++ b/src/data.cc	Tue Apr 17 11:53:39 2012 -0400
@@ -1294,7 +1294,20 @@
         {
           octave_idx_type m = args(1).int_value (), n = args(2).int_value ();
           if (! error_state)
-            retval = arg0.diag ().resize (dim_vector (m, n), true);
+            {
+              if (arg0.is_cell ())
+                {
+                  Cell rhs = arg0.cell_value ();
+                  Cell tmp (m, n);
+
+                  for (octave_idx_type i = 0; i < rhs.numel (); i++)
+                    tmp.xelem (i, i) = rhs.xelem (i);
+
+                  retval = tmp;
+                }
+              else
+                retval = arg0.diag ().resize (dim_vector (m, n), true);
+            }
           else
             error ("diag: invalid dimensions");
         }
@@ -1343,6 +1356,9 @@
 
 ## Test non-square size
 %!assert(diag ([1,2,3], 6, 3), [1 0 0; 0 2 0; 0 0 3; 0 0 0; 0 0 0; 0 0 0])
+%!assert (diag (1, 2, 3), [1,0,0; 0,0,0]);
+%!assert (diag ({1}, 2, 3), {1,[],[]; [],[],[]});
+%!assert (diag ({1,2}, 3, 4), {1,[],[],[]; [],2,[],[]; [],[],[],[]});
 
 %% Test input validation
 %!error <Invalid call to diag> diag ();