|
From: | Fredrik Lingvall |
Subject: | Re: How to create a 3D matrix in an oct-file? |
Date: | Tue, 12 Jan 2010 11:16:39 +0100 |
You didn't set the length to 3. Instead, you created a dim_vector ofOn 12-Jan-2010, Fredrik Lingvall wrote:
| Hi all,
|
| I have tried this:
|
| printf("%d %d %d\n",NN, N_alpha,N_beta);
|
| dim_vector dv (3);
| printf("%d\n",dv.length());
|
| dv(0) = NN;
| dv(1) = N_alpha;
| dv(2) = N_beta;
|
| NDArray p_mat(dv);
| p = p_mat.fortran_vec();
|
| In an oct-file but it fails with:
|
| 20 11 11
| 2
| octave: /usr/local/include/octave-3.2.3/octave/dim-vector.h:125:
| octave_idx_type& dim_vector::dim_vector_rep::elem(int): Assertion `i >= 0 &&
| i < ndims' failed.
| panic: Aborted -- stopping myself...
| Aborted
| Why is the length of the dim_vector only 2 when I set it to 3 in the
| constructor?
length 2 representing an array with 3 rows and 1 column. It's done
this way because all array objects in Octave have at least two
dimensions.
If you want a dim_vector of length 3, you have to write
dim_vector dv;
dv.resize (3);
Or, you can write
dim_vector dv (rows); // This is equivalent to dim_vector (rows, 1);
dim_vector dv (rows, columns);
dim_vector dv (rows, columns, pages);
So far, there are only constructors like this for up to three
dimensions. Above that and you have to write
dim_vector dv;
dv.resize (ndims);
dv[0] = rows;
...
dv[ndims-1] = ...;
OK, I agree that the constructor
dim_vector (octave_idx_type n);
probably misleads people. So perhaps we should remove it to avoid
confusion. Maybe I'm wrong, but I'd guess that there are not very
many legitimate uses of it in Octave.
It might also be useful to have constructors like
NDArray (rows, columns);
NDarray (rows, columns, pages);
and perhaps also for more dimensions (how many?).
If anyone would like to discuss possible changes like this, please
start a new thread on the maintainers list.
jwe
[Prev in Thread] | Current Thread | [Next in Thread] |