Matrix dimensions

 

Hi, suppose I have a matrix defined this way:

#define DIMENSION_2  1

//--- Inside OnTimer( )

double matrix[][DIMENSION_2];

If I want to set the first dimension of the matrix, I can simply use, for instance,

ArrayResize(matrix,4);

However, how can I change the second dimension of the matrix? Is there any option to initialize the matrix with two variable dimensions?

Thanks in advance. 

 
Olá Malacarne! As far as I know, ArrayResize() function allows you to only change the size of the first dimension. 

Anyway, if it is worth the effort of code a workaround, I recommend you take a look at this class of dynamic array: CArrayObj
 
Malacarne:

Hi, suppose I have a matrix defined this way:

If I want to set the first dimension of the matrix, I can simply use, for instance,

However, how can I change the second dimension of the matrix? Is there any option to initialize the matrix with two variable dimensions?

Thanks in advance. 

See also this article https://www.mql5.com/en/articles/567#oop who propose a solution.

If you explain why you need this kind of dynamic array, we can search the best implementation. As you wish.

MQL5 Programming Basics: Arrays
MQL5 Programming Basics: Arrays
  • 2013.03.11
  • Dmitry Fedoseev
  • 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.
 

Hi Figurelli and Alain,

I already came up with a solution... I used two nested for loops and everything works perfectly... It's not elegant, but it works... :-D

Anyway, that's a very intriguing design pattern... I don't know if this is how it's supposed to work in C++...

And Alain, thanks for the interesting link !

P.S.: this matrix library is also very interesting to use in MQL5. 

 
Malacarne:

Hi Figurelli and Alain,

I already came up with a solution... I used two nested for loops and everything works perfectly... It's not elegant, but it works... :-D

Anyway, that's a very intriguing design pattern... As I'm not a programmer, I don't know if this is how it's supposed to work in C++...

And Alain, thanks for the interesting link !

P.S.: this matrix library is also very interesting to use in MQL5. 

In C+ dynamic arrays are implemented with pointers. There is no equivalent in mql5.