help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Appending to a ColumnVector


From: John W. Eaton
Subject: Re: Appending to a ColumnVector
Date: Sat, 4 Jun 2005 00:14:16 -0400

On  3-Jun-2005, Søren Hauberg wrote:

| Hi, sorry about the late reply...
| 
| John W. Eaton wrote:
| > On 25-May-2005, Søren Hauberg wrote:
| > 
| > | Michael Creel wrote:
| > | [snip]
| > | > From 
| > | > 
http://pareto.uab.es/mcreel/OctaveClassReference/html/classColumnVector.html 
| > | > there is the entry
| > | > ColumnVector ColumnVector::stack   (   const ColumnVector &   a    )    
const
| > | > which looks like it might help. I don't see any resize_and_fill 
operation 
| > | > anywhere - does this work with more or less current versions of Octave?
| > | > M.
| > | I'm assuming that stack concatenates two ColumnVectors, which seems a 
| > | bit overkill since I simply want to append one value. The function 
| > | resize_and_fill is part of the Array class, so ColumnVector inherits 
| > | this operation.
| > 
| > There should probably also be functions like
| > 
| >   ColumnVector ColumnVector::append (double val) const;
| I'm not very familiar with C++ (I'm a C-man) but shouldn't this return a 
| ColumnVector& ?
| >   ColumnVector& ColumnVector::append (double val);
| Anyway, as I'm not really used to working with the "core" of Octave, I 
| have to ask: Is this what you're looking for?
| 
| ColumnVector&
| ColumnVector::append (const double val)
| {
|    const int len = length();
|    ColumnVector &c = *new ColumnVector(len+1);
| 
|    for (int i = 0; i < len; i++)
|      c(i) = xelem(i);
| 
|    c(len) = val;
|    return c;
| }

No, the non-const version should resize and return *this.  Probably
something more like the following (untested):

  ColumnVector&
  ColumnVector::append (double val)
  {
    octave_idx_type len = length ();

    resize (len+1);

    xelem(len) = val;

    return *this;
  }

The const version should return a new vector.  Given the non-const
version above, you could write

  ColumnVector
  ColumnVector::append (double val) const
  {
    ColumnVector retval = *this;
    return retval.append (val);
  }

but this would copy the data twice, so you can do better by creating
an empty array of the proper size and copying the data yourself.
There are some macros for this kind of thing in Octave already, so you
should not have to write a loop.  Something like

  ColumnVector&
  ColumnVector::append (double val)
  {
    octave_idx_type len = length ();

    ColumnVector retval (len+1);

    double *r = retval.fortran_vec ();

    const double *d = data ();

    mx_inline_copy (r, d, len);

    r[len] = val;

    return retval;
  }

will probably be faster.  The mx_inline_* macros are defined in
liboctave/mx-inlines.cc.

jwe



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

[Prev in Thread] Current Thread [Next in Thread]