help-octave
[Top][All Lists]
Advanced

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

Re: OCTAVE: Is Array3 available as a 3D array datatype in Octave


From: Ted Harding
Subject: Re: OCTAVE: Is Array3 available as a 3D array datatype in Octave
Date: Wed, 9 Jul 1997 20:39:35 +0100 (GMT+0100)

( Re Message From: Braddock Gaskill )
> 
> A 3-dimensional array datatype would be infinitely useful to me in Octave.
> 
> I'm interested in either knowing how to use one, or implementing one
> myself.  I see there is an Array3 class in liboctave/Array3.cc, but there
> is no documentation on how to use it in the octave interpreter (if that's
> possible...there is only one reference to the class in a 
> `grep Array3 src/*.cc`).
> 
> Is there any way to implement a 3D array in Octave?  The closest I can
> figure out using the obvious datatypes would be to make a data structure
> of matricies with some nasty eval's to access the elements.
> 
> Thanks a googol!
> 
>       -Braddock

It ain't quite as bad as that ... You can take a hint from C, where an
array is a linear block accessed by a computed pointer. The following is a
skeleton example to implement a d-dimensional array, exemplified with d=4.
Maybe it could be a touch more elegant, and it needs array-bound checks
and such before it's fit to be let out on its own, but you get the idea.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
A.n_dims = 4;
A.dims = [2;2;3;3];
A.elems = [
  1111; 1112; 1113; 1121; 1122; 1123; 1131; 1132; 1133;
  1211; 1212; 1213; 1221; 1222; 1223; 1231; 1232; 1233;
  2111; 2112; 2113; 2121; 2122; 2123; 2131; 2132; 2133;
  2211; 2212; 2213; 2221; 2222; 2223; 2231; 2232; 2233; ];

function x = elem(X,j) # X a d-dim array, j a d-vector of "coordinates"
  d = X.n_dims; k = X.dims; k = flipud(cumprod(flipud(k)));
  i = j(d); for r=1:d-1, i = i+(j(r)-1)*k(r+1); endfor
  x = X.elems(i);
endfunction
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
octave-2:13> elem(A,[1;1;1;1])
ans = 1111
octave-2:14> elem(A,[1;1;2;1])
ans = 1121
octave-2:15> elem(A,[2;1;3;2])
ans = 2132
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
You could also try further tricks (e.g. using NaN) to access sub-arrays,
e.g.  elem(A,[1,NaN,NaN,NaN]) would return the 2x3x3 array with i1=1,
elem(A,[1,NaN,3,NaN) the 2x3 with i1=1, i3=3
(not forgetting to recompute .n_dims and .dims).

Hope this helps,
Ted.                                    (address@hidden)

reply via email to

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