Indexing array elements inside a cell in C++ API

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Indexing array elements inside a cell in C++ API

rupakrokade
In octave if you do c = {[1;2]}, it will result in the following output.
*c =
{
  [1,1] =

     1
     2

}*

The problem is I do not know a way to access the second element of the
array. Doing c(2,1) says

*error: c(2,_): but c has size 1x1*

Notice that it says the size is 1x1 whereas the array is clearly of size
2x1.

I actually want to index the array inside the cells within a C++ code using
Octave's API. *feval()* returns me a cell containing such an array and I am
struggling to index it. Please help.



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Indexing array elements inside a cell in C++ API

Mariano
You can try with cell2mat or {}()
>> c={[1;2]}
c =
{
  [1,1] =

     1
     2

}

>> cell2mat(c)(2,1) % Using cell2mat
ans =  2
>> c{1}(2,1) % Using {}()
ans =  2



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Indexing array elements inside a cell in C++ API

Pantxo
In reply to this post by rupakrokade
rupakrokade wrote

> In octave if you do c = {[1;2]}, it will result in the following output.
> *c =
> {
>   [1,1] =
>
>      1
>      2
>
> }*
>
> The problem is I do not know a way to access the second element of the
> array. Doing c(2,1) says
>
> *error: c(2,_): but c has size 1x1*
>
> Notice that it says the size is 1x1 whereas the array is clearly of size
> 2x1.
>
> I actually want to index the array inside the cells within a C++ code
> using
> Octave's API. *feval()* returns me a cell containing such an array and I
> am
> struggling to index it. Please help.
>
>
>
> --
> Sent from:
> https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html

The top container, the cell array, actually only contains 1 element, the
double vector. In  Octave you would have to do

vec = c{1};
vec(2,1);

In C++, you'll want to do something equivalent. Suppose args is the input
octave_value_list and contains your cell variable:


#include <octave/oct.h>

DEFUN_DLD(toto, args, nargout,
          "-*- texinfo -*-\n\
@deftypefn {} {@var{retval} =} toto (@var{input1})\n\
@seealso{}\n\
@end deftypefn")
{
  octave_value_list retval;

  if (args.length () < 1)
    error ("toto: expects one argument");
 
  Cell c = args(0).xcell_value ("First argument should be a cell");

  if (c.numel () < 1)
    error ("toto: expect the cell to contain a matrix");
 
  Matrix m = c(0).xmatrix_value ("First element of the cell should be a
matrix");
  octave_stdout << "First element of the matrix : "<< m(0,0) << std::endl;
 
  return retval;
}

HTH,

Pantxo



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: Indexing array elements inside a cell in C++ API

rupakrokade
Thank you all for your helpful responses.



--
Sent from: https://octave.1599824.n4.nabble.com/Octave-General-f1599825.html