# HG changeset patch # User John W. Eaton # Date 1334678019 14400 # Node ID 5bb5fcffa29d0bf1abdb668d471ec0e5f44a74ae # Parent aa491bd9e19b56aad9548089fd8f8a2dc1a2be24 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 []. diff -r aa491bd9e19b -r 5bb5fcffa29d src/data.cc --- 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 diag ();