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

Re: Dimensional Arrays Do Not Work



PureBytes Links

Trading Reference Links

Bob Fulks wrote:
> My application was that I had a variable that was a function of
> three other variables: 
>    Y = function of (S, J, K)
> The relationship I wanted was pretty complex and the variables 
> S, J, and K could only have the integer values of 0 through 7. So I
> thought I would just use a look-up table of the values rather than
> try to approximate the relationship with equations. 
> To do this I would use a 3 dimensional array which would look like
> a cube with 8 cells on each edge, for a total of 512 cells
> (= 8 * 8 * 8). 

I suspect Bob has already thought of this, but for the benefit of 
others on the list:

You can simulate a 3-dimensional array using a 1-dimensional array.  
You just have to do the array-indexing calculations yourself, but 
that's pretty simple.

In Bob's case he would need a 512-element array.  Then you would just 
multiply S and J by appropriate amounts to offset the array 
references into the correct part of the 1-dimensional array.  

For an 8 * 8 * 8 array, you could do something like this:

Array:  Table[511];  { Table contains elements 0 through 511 }

{ To access element T[S, J, K] of the array, you would say: }

X = Table[S*64 * J*8 + K];

Since S, J, and K range from 0 to 7 in Bob's example, that equation 
will produce indices fro 0 to 511.  That index calculation is what a 
computer language automatically does for you (or at least is SUPPOSED 
to do for you :-) when you use a multi-dimensional array.

Gary