help-octave
[Top][All Lists]
Advanced

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

Access data in an NDArray


From: John W. Eaton
Subject: Access data in an NDArray
Date: Thu, 31 Aug 2006 10:18:44 -0400

On 31-Aug-2006, Fredrik Lingvall wrote:

| For a Matrix I can get a pointer to the data using, for example,
| 
| const Matrix tmp = args(0).matrix_value();
| A_M = tmp.rows();
| A_N = tmp.cols();
| A = (double*) tmp.fortran_vec();

You do not need the cast here.  If you aren't trying to modify the
data (and if you really are using the argument to a function, then
you shouldn't be), then you might want to use the data method
instead of fortran_vec so that you don't generate a copy of the data:

  const Matrix tmp = args(0).matrix_value ();
  octave_idx_type a_m = tmp.rows ();
  octave_idx_type a_n = tmp.cols ();
  const double *a = tmp.data ();

| Now I want to do that for a (3D) NDArray, that is, I want
| a pointer to each (2D) Matrix in the NDArray. How do
| I do that? Is the data stored in one consecutive block so
| that I can use pointer arithmetics to find each Matrix in
| the NDArray?

Yes, the data is still stored in a single contiguous block in column
major order, so you can write

  const NDArray tmp = args(0).array_value ();
  octave_idx_type a_m = tmp.rows ();
  octave_idx_type a_n = tmp.cols ();
  octave_idx_type a_p = tmp.pages ();
  const double *a = tmp.data ();

in place of rows, cols (or columns), and pages, you can also use dim1,
dim2, and dim3.  Beyond 3 dimensions, you have to work with the
elements of the dim_vector:

  dim_vector dv = tmp.dims ();

  octave_idx_type d1 = dv(0);  // Or call it d0 if you prefer.
  ...                          // ...
  octave_idx_type d4 = dv(3);  // Or call it d3 if you prefer.

jwe


reply via email to

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