help-octave
[Top][All Lists]
Advanced

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

RE: Three and higher dimensional arrays


From: Ted Harding
Subject: RE: Three and higher dimensional arrays
Date: Wed, 15 Aug 2001 21:43:54 +0100 (BST)

On 15-Aug-01 Scott Walck wrote:
> Is there a way to produce three or higher dimensional arrays in octave?
> The obvious ways don't seem to work.
> x(1,2,4) = 7
> x(1,2)(4) = 7
> etc.
> Thanks for any help and sorry if this question has been asked before
> (or again and again and again).

Well, it has been asked a few times (I'm not sure how many "agains").
One way is to simulate it by computing an index into a 1-dim array.
And it's convenient to use a "structure" as the object which encapsulates
your "n-dimensional array". Here's an answer I gave back in 1999:
==========================
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;          # 3 dimensions
octave:12> A.dims = [3 2 4];    # dimensions of lengths 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 ];   # assign the elements
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                              # function to access them
octave:15> elem(A,1,2,3)
ans = 123
octave:16> elem(A,2,2,4)
ans = 224

(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)
==========================

I hope this helps!

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <address@hidden>
Fax-to-email: +44 (0)870 167 1972
Date: 15-Aug-01                                       Time: 21:43:54
------------------------------ XFMail ------------------------------



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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