help-octave
[Top][All Lists]
Advanced

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

Passing integers: use arg(x).int_value() ?


From: Steve C. Thompson
Subject: Passing integers: use arg(x).int_value() ?
Date: Mon, 21 Feb 2005 11:28:09 -0800
User-agent: Mutt/1.3.28i

Hello Group,

I'm trying to pass integers into a custom .oct program.  For example
(granted, a useless program), say I want to generate a matrix of
random numbers then return the matrix, the number or rows and the number of
columns both of which are passed to the function. I've found that these
parameters must be of type ``int'' or the ``octave_rand'' gets mad.
With some trial and error, I've found that ``args(x).int_value() works.
Is this the best/simplest way to do this?

Below is a sample program.

#include <octave/oct.h>
#include <octave/oct-rand.h>
DEFUN_DLD (sct_rand, args, ,
  "Returns a matrix of random numbers")
{ 
octave_value_list retval;

  int r = args(0).int_value();
  int c = args(1).int_value();

  octave_rand::distribution ("normal");

  Matrix tmp = octave_rand::matrix (r, c);

  retval(0) = r;
  retval(1) = c;
  retval(2) = tmp;

  return retval;
}

(For simplicity, no texinfo.)

Please reply if you see a needed correction in implementation.  Also, I
appreciate any corrections in coding style, terminology, etc.

Steve
http://elsteve.com/test_oct/

--------------------------------------------------

P.S.

Compile with: mkoctfile sct_rand.cc
In Octave, run

[a,b,c]=sct_rand(5,3)

to get something like

a = 5
b = 3
c =

   0.628785   2.195041  -0.357113
  -1.120253   0.348510   0.083933
  -1.580816   0.379169  -0.179929
  -1.216113  -0.527883  -0.328695
  -1.564885  -0.271078  -0.832184

--------------------------------------------------



-------------------------------------------------------------
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]