Converting a 4D array to a 1D array and addressing it

  • Thread starter Thread starter elziko
  • Start date Start date
E

elziko

I have a four dimensional array which I am trying to flatten into a single
dimension array:

Dim Array4D(10,10,10,10) as Single

I can create my one dimensional array to be big enough to contain teh data
from the 4D array:

Dim Array2D(Array4D.Length - 1) As Single

Then I populate it like so:

Dim intArray2dIndex As Integer
For t As Integer = 0 To Array4D.GetUpperBound(0)
For x As Integer = 0 To Array4D.GetUpperBound(1)
For y As Integer = 0 To Array4D.GetUpperBound(2)
For z As Integer = 0 To Array4D.GetUpperBound(3)
Array2D(intArray2dIndex) = Array4D(t, x, y, z)
intArray2dIndex += 1
Next
Next
Next
Next

But once I have my 1D array how do I retrieve a value from it using the
original t,x,y,z values? In other words, given a t, x, z and y value that
would describe a position in the 4D array I need to translate that to a
single value that describes the position in the one dimensional array.

Any help on this one?

TIA
 
Just out of interest, if you are trying to represent homogenous coordinates,
why not create a single Vertex class and have an array of Vertices?

public class Vertex

public x, y, z, t as single

end class

Dim myArray(1) as Vertex

for each theVertex as Vertex in myArray
theVertex.x = 10.0
next
 
I have a four dimensional array which I am trying to flatten into a single
dimension array:

Dim Array4D(10,10,10,10) as Single

I can create my one dimensional array to be big enough to contain teh data
from the 4D array:

Dim Array2D(Array4D.Length - 1) As Single

Then I populate it like so:

Dim intArray2dIndex As Integer
For t As Integer = 0 To Array4D.GetUpperBound(0)
For x As Integer = 0 To Array4D.GetUpperBound(1)
For y As Integer = 0 To Array4D.GetUpperBound(2)
For z As Integer = 0 To Array4D.GetUpperBound(3)
Array2D(intArray2dIndex) = Array4D(t, x, y, z)
intArray2dIndex += 1
Next
Next
Next
Next

But once I have my 1D array how do I retrieve a value from it using the
original t,x,y,z values? In other words, given a t, x, z and y value that
would describe a position in the 4D array I need to translate that to a
single value that describes the position in the one dimensional array.

Any help on this one?

TIA

First work out out on paper how you would flatten a 2D array into a 1D
array, and how you would retrieve the array entries given the two
indices. Program your solution and test it.

Next do the same for flattening a 3D array. Test your solution so you
are sure that it works.

After doing that you should be in a position to solve your problem
with a 4D array.

rossum





The ultimate truth is that there is no ultimate truth
 
A simplified piece of code would be:

offset1 = Array4D.GetUpperBound(1) * Array4D.GetUpperBound(2) * _
Array4D.GetUpperBound(3)
offset2 = Array4D.GetUpperBound(2) * Array4D.GetUpperBound(3)
offset3 = Array4D.GetUpperBound(3)

Each element would be at

value = Array2D((t*offset1) + (x*offset2) +(y*offset3) + z)

Cheers,
Jason
 
Just out of interest, if you are trying to represent homogenous
coordinates, why not create a single Vertex class and have an array of
Vertices?

I cannot do this because I am writing to a propriety file format using a 3rd
party DLL requiring me to start with a 1D array of singles.
 
Back
Top