[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Squeeze works differently in Matlab
From: |
John W. Eaton |
Subject: |
Squeeze works differently in Matlab |
Date: |
Tue, 3 Aug 2004 15:56:24 -0400 |
On 2-Jul-2004, Andy Adler <address@hidden> wrote:
| I'm trying to get some code to run under Matlab and Octave,
| and I note that 'squeeze' works differently, if there is only
| one non-singleton dimention:
|
| Matlab:
| >> squeeze(rand(2,1,3))
| ans =
| 0.4447 0.7919 0.7382
| 0.6154 0.9218 0.1763
|
| >> squeeze(rand(1,1,3))
| ans =
| 0.4057
| 0.9355
| 0.9169
|
| Octave (2.1.57 - today's CVS):
| octave:1> squeeze(rand(2,1,3)) # This is OK
| ans =
| 0.47701 0.72476 0.80614
| 0.64084 0.87862 0.71905
|
| octave:2> squeeze(rand(1,1,3)) # This is transposed wrt Matlab
| ans =
| 0.839686 0.112393 0.016944
I also see that squeeze has no effect on 2-d arrays so that squeezing
a row vector does not convert it to a column vector. Hmm. So to be
contemptible, the algorithm should really be
if (ndims > 2)
remove all singleton dimensions;
if (ndims == 0)
dims = [1, 1];
elseif (ndims () == 1)
dims = [dims, 1];
endif
endif
? Would this produce the desired results in all cases? If so, then
please try the following patch.
Thanks,
jwe
liboctave/ChangeLog:
2004-08-03 John W. Eaton <address@hidden>
* Array.cc (Array<T>::squeeze): Do nothing for 2-d arrays. For
arrays with more than two dimensions and only one non-singleton
dimension, return a column vector.
Index: liboctave/Array.cc
===================================================================
RCS file: /usr/local/cvsroot/octave/liboctave/Array.cc,v
retrieving revision 1.119
diff -u -r1.119 Array.cc
--- a/liboctave/Array.cc 26 Jul 2004 14:37:05 -0000 1.119
+++ b/liboctave/Array.cc 3 Aug 2004 19:56:00 -0000
@@ -71,50 +71,45 @@
{
Array<T> retval = *this;
- bool dims_changed = false;
+ if (ndims () > 2)
+ {
+ bool dims_changed = false;
- dim_vector new_dimensions = dimensions;
+ dim_vector new_dimensions = dimensions;
- int k = 0;
+ int k = 0;
- for (int i = 0; i < ndims (); i++)
- {
- if (dimensions(i) == 1)
- dims_changed = true;
- else
- new_dimensions(k++) = dimensions(i);
- }
+ for (int i = 0; i < ndims (); i++)
+ {
+ if (dimensions(i) == 1)
+ dims_changed = true;
+ else
+ new_dimensions(k++) = dimensions(i);
+ }
- if (dims_changed)
- {
- switch (k)
+ if (dims_changed)
{
- case 0:
- new_dimensions = dim_vector (1, 1);
- break;
+ switch (k)
+ {
+ case 0:
+ new_dimensions = dim_vector (1, 1);
+ break;
- case 1:
- {
- int tmp = new_dimensions(0);
+ case 1:
+ {
+ int tmp = new_dimensions(0);
- new_dimensions.resize (2);
+ new_dimensions.resize (2);
- if (dimensions(0) == 1)
- {
- new_dimensions(0) = 1;
- new_dimensions(1) = tmp;
- }
- else
- {
new_dimensions(0) = tmp;
new_dimensions(1) = 1;
}
- }
- break;
+ break;
- default:
- new_dimensions.resize (k);
- break;
+ default:
+ new_dimensions.resize (k);
+ break;
+ }
}
retval.make_unique ();
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- Squeeze works differently in Matlab,
John W. Eaton <=