News:

  • August 14, 2026, 11:49:44 PM

Login with username, password and session length

Author Topic: 2-D Matrix (Array)  (Read 7085 times)

RBPLC

  • Hero Member
  • *****
  • Posts: 586
2-D Matrix (Array)
« on: September 29, 2019, 03:53:08 PM »
What is the "best" way to work with a 2-D Matrix? For example, in the Productivity series I can assign a data type of 2D Array of floats (for example FloatArray). I can then reference elements in this matrix in the following manner: FloatArray(2,5) for example. What is the "best" equivalent way to do this in DMD?   

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6170
  • Yes Pinky, Do-more will control the world!
Re: 2-D Matrix (Array)
« Reply #1 on: September 29, 2019, 08:19:44 PM »
Do-more doesn?t yet support arrays with more than one dimension, but it is quite easy to simulate two or more dimensions with MATH. In your example, FloatArray(2, 5) (where 2 is the row and 5 is column) becomes FloatArray[2*ROWSIZE + 5]. ROWSIZE should ideally be a symbolic constant set to the actual width of the matrix. In a MATH box, the expression in the brackets can be whatever you want it to be, so many more dimensions are possible. If you wanted a 3 dimension array, with axes X, Y, and Z, it would be DataCube[Z*ZSIZE + Y*YSIZE + X]. Since MATH also supports an expression in the output parameter, you can fill the array using the same mechanism.

So if you wanted a 10 x 10 array, allocate a memory block that 10 x 10 = 100 elements. I?ll call it MyArray. Create a symbolic constant YSIZE = 10, and YIdx and XIdx are just nicknames for your index variables. Then you can use loops or whatever to put data into the array. A loop might look like this:

FOR YIdx = 0 to 10
    FOR XIdx = 0 to 10
         MATH MyArray[YIdx*YSIZE + XIdx] = Whatever I want to store in the array.
    NEXT
NEXT

Accessing the data works the same way. You just specify the multidimensional array math on the right side of the equal, instead of the left.

We do have longer term plans to fully support n dimension arrays, but not sure when that happens.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO