help-octave
[Top][All Lists]
Advanced

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

Re: Creating double hypermatrix in C++


From: c.
Subject: Re: Creating double hypermatrix in C++
Date: Tue, 18 Oct 2016 14:29:22 +0200

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

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