Unable to dynamically declare a bidimensional array

 

As the title said, I'm getting this annoying error for something that should be trivial, why can't I use variables to declare the dimensions of an array ?

Practically I do not know the values for the dimensions as mat would be declared with random values that range between at least 50 and 200.

Can someone enlighten me on how can I achieve the same result in mql5 ?

 

Take a look at this article. Multidimensional arrays can only be dynamic on the first dimension, while the others must be static.


I'm not quite used to them, but I believe you can use matrices pretty much as how you would use 2-dimension arrays:


#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
    matrix mat;
    
    mat.Reshape(3,3);
    
    mat[0][0] = 123;
    double val = mat[0][0];
    
    Print("Value = ", val);
    
  }
//+------------------------------------------------------------------+
MQL5 Programming Basics: Arrays
MQL5 Programming Basics: Arrays
  • www.mql5.com
Arrays are an integral part of almost any programming language along with variables and functions. The article should be of interest primarily to novice MQL5 programmers, while experienced programmers will have a good opportunity to summarize and systematize their knowledge.
 
Emanuel Cavalcante Amorim Filho #:

Take a look at this article. Multidimensional arrays can only be dynamic on the first dimension, while the others must be static.


I'm not quite used to them, but I believe you can use matrices pretty much as how you would use 2-dimension arrays:


It's just not only about the first dimension, in order to declare from the second dimension onward, only constant values ( numbers ) are allowed, I tried to use variables to declare a second and a third dimension and it still didn't let me.

For the matrices I tried to used them, even though my logic checks out, for my specific use case I was not able to use them as general input into an ai model. In my case I had an input shape { 101, 200, 2 }, I complement this with an array of matrixf with 200 rows and 2 columns, I do believe that when setting the input shape for the model, despite having all the necessary data, when doing the check on the size of what the model is expecting and the new structure of matrix, the dimension do not checks out. 

In the end I was able to solve my problem by refactoring my model by using constant values for the dimension of a multi-dimensional arrays used as input structure ( Ugly af ).

Is there a better way to make complex structures of dimensions not known at runtime ?
 
Jacopo Mendoza Betances #:

It's just not only about the first dimension, in order to declare from the second dimension onward, only constant values ( numbers ) are allowed, I tried to use variables to declare a second and a third dimension and it still didn't let me.

For the matrices I tried to used them, even though my logic checks out, for my specific use case I was not able to use them as general input into an ai model. In my case I had an input shape { 101, 200, 2 }, I complement this with an array of matrixf with 200 rows and 2 columns, I do believe that when setting the input shape for the model, despite having all the necessary data, when doing the check on the size of what the model is expecting and the new structure of matrix, the dimension do not checks out. 

In the end I was able to solve my problem by refactoring my model by using constant values for the dimension of a multi-dimensional arrays used as input structure ( Ugly af ).

Is there a better way to make complex structures of dimensions not known at runtime ?
Yes there is, actually.

Think about what a multidimensional array is.

You can use a one dimensional array and access it in a multidimensional way, and for that you can use any geometry you like. Actually, any multidimensional array is in fact just a one dimensional array, but with a predefined access pattern.

You can easily create a function that converts from multidimensional coordinates into one dimensional indexing.

Your NN does that, that's why you have to specify the dimensions.