help-octave
[Top][All Lists]

## Re: Creating double hypermatrix in C++

 From: Shamika Mohanan Subject: Re: Creating double hypermatrix in C++ Date: Wed, 19 Oct 2016 10:49:25 +0530

Hello,

It works if the dimensions are specified. I had to change a to 0 based indexing. What does 4 refer to in dim_vector d(4, iRows, iCols);? Number of matrices?

Shamika

On Tue, Oct 18, 2016 at 5:59 PM, c. wrote:

On 18 Oct 2016, at 12:22, Shamika Mohanan <address@hidden> wrote:

> Hello,
>
> I'm trying to create a 3D double matrix using Octave API. This is what I have so far.
>
> NDArray ndarray_double;
> for( a= 1 ;a<=3 ; a++)
> {
>   for(b=0;b<iRows;b++)
>   {
>    for (c=0;c<iCols;c++)
>    {
>     ndarray_double(b,c,a)=(listItem[b+iRows*c]);
>    }
>   }
> }
> dim_vector d = (ndarray_double.dims());
> octave_value_list in=octave_value (ndarray_double);
>
> I assigned a 3x3x3 matrix  to ndarray_double. When I printed the value of d, I got 0,0,32.
>
> 1. Did I initialize and assign values to a double hypermatrix correctly?
> 2. Is dims() the correct way to get the dimensions of an NDArray?
>
> Regards,
> Shamika

try the following:

---8<---

#include <octave/oct.h>

constexpr int iCols = 3;
constexpr int iRows = 3;

DEFUN_DLD (pippo, args, nargout,
"Help String")
{
dim_vector d(4, iRows, iCols);
NDArray ndarray_double (d, 0.0);
for (octave_idx_type a = 1 ; a <= 3; a++) // Is this really what you want to do??
for (octave_idx_type b = 0; b < iRows; b++)
for (octave_idx_type c = 0; c < iCols; c++)
ndarray_double (b, c, a) = static_cast<double> (a + b + c);

return (octave_value (ndarray_double));
}

---8<---

your cycle on 'a' looks suspicious, indexing is 0-based in the C++ API.

c.

reply via email to