changeset 9046:88bf56bbccca

make Array::find already return Matlab-compatible dimensions
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 27 Mar 2009 12:22:05 +0100
parents ac0a23e9f5c5
children a1635f7c4cbe
files liboctave/Array.cc liboctave/Array.h liboctave/ChangeLog src/ChangeLog src/DLD-FUNCTIONS/find.cc
diffstat 5 files changed, 27 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/liboctave/Array.cc
+++ b/liboctave/Array.cc
@@ -2546,6 +2546,21 @@
         }
     }
 
+  // Fixup return dimensions, for Matlab compatibility.
+  // find(zeros(0,0)) -> zeros(0,0)
+  // find(zeros(1,0)) -> zeros(1,0)
+  // find(zeros(0,1)) -> zeros(0,1)
+  // find(zeros(0,X)) -> zeros(0,1)
+  // find(zeros(1,1)) -> zeros(0,0) !!!! WHY?
+  // find(zeros(0,1,0)) -> zeros(0,0)
+  // find(zeros(0,1,0,1)) -> zeros(0,0) etc
+
+  if ((numel () == 1 && retval.is_empty ())
+      || (rows () == 0 && dims ().numel (1) == 0))
+    retval.dimensions = dim_vector ();
+  else if (rows () == 1 && ndims () == 2)
+    retval.dimensions = dim_vector (1, retval.length ());
+
   return retval;
 }
 
--- a/liboctave/Array.h
+++ b/liboctave/Array.h
@@ -628,6 +628,7 @@
     return result;
   }
 
+  template <class U> friend class Array;
 };
 
 #endif
--- a/liboctave/ChangeLog
+++ b/liboctave/ChangeLog
@@ -1,3 +1,8 @@
+2009-03-26  Jaroslav Hajek  <highegg@gmail.com>
+
+	* Array.cc (Array<T>::find): Reshape result for Matlab compatibility.
+	* Array.h (Array<T>): Add friend template declaration.
+
 2009-03-27  Jaroslav Hajek  <highegg@gmail.com>
 
 	* idx-vector.cc (idx_vector::idx_vector_rep::idx_vector_rep (const
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,9 @@
+2009-03-26  Jaroslav Hajek  <highegg@gmail.com>
+
+	* DLD-FUNCTIONS/find.cc 
+	(find_nonzero_elem_idx (const Array<T>&, ...)): Move dimensions
+	fixup to liboctave.
+
 2009-03-26  Jaroslav Hajek  <highegg@gmail.com>
 
 	* DLD-FUNCTIONS/find.cc 
--- a/src/DLD-FUNCTIONS/find.cc
+++ b/src/DLD-FUNCTIONS/find.cc
@@ -49,23 +49,6 @@
   else
     idx = nda.find ();
 
-  // Fixup idx dimensions, for Matlab compatibility.
-  // find(zeros(0,0)) -> zeros(0,0)
-  // find(zeros(1,0)) -> zeros(1,0)
-  // find(zeros(0,1)) -> zeros(0,1)
-  // find(zeros(0,X)) -> zeros(0,1)
-  // find(zeros(1,1)) -> zeros(0,0) !!!! WHY?
-  // find(zeros(0,1,0)) -> zeros(0,0)
-  // find(zeros(0,1,0,1)) -> zeros(0,0) etc
-  // FIXME: I don't believe this is right. Matlab seems to violate its own docs
-  // here, because a scalar *is* a row vector.
-
-  if ((nda.numel () == 1 && idx.is_empty ())
-      || (nda.rows () == 0 && nda.dims ().numel (1) == 0))
-    idx = idx.reshape (dim_vector (0, 0));
-  else if (nda.rows () == 1 && nda.ndims () == 2)
-    idx = idx.reshape (dim_vector (1, idx.length ()));
-
   switch (nargout)
     {
     default: