help-octave
[Top][All Lists]
Advanced

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

Re: Global array variable from an OCT file example


From: mpender
Subject: Re: Global array variable from an OCT file example
Date: Thu, 9 Jan 2014 21:28:47 -0800 (PST)

I haven't received much response, so I thought I would provide more specific
detail.  The code of my .oct file appears below.  The array PFITNESS is
initialized ahead of time as an array of single precision floats, but the
method I'm using with declaring an NDArray tmp6 and then assigning tmp6 to
the PFITNESS global at the end results in replacing the original array of
single precision floats with a new array of doubles.  Clearly this is not
what I intended.

What I would like to do is reach into the preallocated array of singles and
overwrite the appropriate values without repeatedly requesting new memory to
request the same array.  Also, if anyone knows what matrix constructor I can
use for an array of single precision floats instead of NDArray, which
creates double precision floats, I would appreciate that information as
well.

Thanks, Mike

--- Code follows ---

#include <octave/oct.h>

DEFUN_DLD (computefitness, args, nargout,  "Create a vector of PFITNESS
values of size POPULATIONSIZE.")
{
  octave_value_list retval;
  int nargin = args.length();

  if (nargin != 2)
     print_usage ();
  else
  {
     const int popsize = args(0).int_value();
     const int genelen = args(1).int_value();

     octave_value tmp1 = get_global_value ("POPULATION", true);
     NDArray tmp2 = tmp1.array_value();

     octave_value tmp3 = get_global_value ("GOALMAT", true);
     NDArray tmp4 = tmp3.array_value();

     octave_value tmp5 = get_global_value ("PFITNESS", true);
// This constructor creates an array of doubles instead of singles, like the
original.
// That is wasteful and should be revised in a future version.
     NDArray tmp6 = tmp5.array_value();

     unsigned int count, indx;
     for (indx = 0; indx < popsize; indx++)
     {
        count = 0 ;
        for (int jndx = 0; jndx < genelen; jndx++)
           if (tmp2(indx, jndx) == tmp4(indx, jndx))
              count++;
        tmp6(indx, 0) = ((float) count) / ((float) genelen);
     }

// Assign the memory space to the global variable
    set_global_value ("PFITNESS", tmp6);
   }
   return octave_value_list();
}




--
View this message in context: 
http://octave.1599824.n4.nabble.com/Global-array-variable-from-an-OCT-file-example-tp4660831p4660895.html
Sent from the Octave - General mailing list archive at Nabble.com.


reply via email to

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