help-octave
[Top][All Lists]

## Re: [Q] multidimensional array?

 From: Joao Cardoso Subject: Re: [Q] multidimensional array? Date: Wed, 24 Mar 1999 15:01:58 +0000

```(Ted Harding) wrote:
>
> On 24-Mar-99 Jae-Hoon Jeong wrote:
> > Hello.
> > Is it possible using mutidimensional array more than 2
> > in octave?
>
> Although it is not possible to do it directly, in the form A(i,j,k), it
> can be simulated by computing an index into a linear array stored in a
> structure:
>
> octave:11> A.ndim = 3;
> octave:12> A.dims = [3 2 4];
> octave:13> A.elems = ...
> >  [111 112 113 114 121 122 123 124 \
> >   211 212 213 214 221 222 223 224 \
> >   311 312 312 314 321 322 323 324 ];
> octave:14> function y = elem(A,i,j,k)
> > y = A.elems(k + A.dims(3)*(j-1) + A.dims(3)*A.dims(2)*(i-1));
> > endfunction
> octave:15> elem(A,1,2,3)
> ans = 123
> octave:16> elem(A,2,2,4)
> ans = 224

I rather use lists:

a=list(rand(3),eye(3),ones(3))
nth(a,1)(3,2)
sum(nth(a,1)(3,:))

Joao

>
> (where the last index varies most rapidly along the linear array and the
> first least rapidly).
>
> The function definition gets a bit more complicated if you have to allow
> for arbitrary numbers of dimensions (use varargs)
>
> Ted.
>
> --------------------------------------------------------------------
> Date: 24-Mar-99                                       Time: 11:58:30
> ------------------------------ XFMail ------------------------------

--