help-octave
[Top][All Lists]
Advanced

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

RE: creating a NxMxW matrix


From: Ted Harding
Subject: RE: creating a NxMxW matrix
Date: Wed, 26 Apr 2000 11:00:37 +0100 (BST)

On 25-Apr-00 Francisco Rojas wrote:
> 
> I'm trying to create a matrix of (A,B,C)
>  using 3 for loops
> 
>   for x=1:5,
>> for Y=1:10,
>> for Z=1:3,
>> loop(x,Y,Z)=x*Y*Z
>> end
>> end
>> end
> 
> I get the following error message:
> error: invalid number of indices (3) for indexed matrix assignment
> 
> Can someone point me as to the proper way of creating this ?

As far as I know, octave still only implements 2-dimensional arrays.

The following is a method which I use in octave when p-dimensional
arrays are required (exemplified for p=3):

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

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

If you need to make heavy use of p-dimensional arrays (p up to 10)
you might consider looking at the package "tela" (short for "Tensor
Language") which is very similar in many ways to octave (and MatLab),
but designed to handle multidimensional arrays.

Have a look at ftp.funet.fi:pub/sci/math/tela/

I have found tela useful on occasion, but don't do enough p-dimensional
work to feel under pressure to use it regularly, rather than octave
(with the above emulation of p-dim arrays).

I hope this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <address@hidden>
Date: 26-Apr-00                                       Time: 11:00:37
------------------------------ XFMail ------------------------------



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

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



reply via email to

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