dynamic allocation of a multidimensional matrix

Hi, it's my first post. Sorry if i done anything wrong.

My question is really straightfoward, i need to creat a n! multidimensional matrix for exemple: if the user put a input n = 5 them i need to creat a matrix like that : double matrix[5][4][3][2][1], and so on:
double matrix[n][n-1][n-2][n-3]... . There's any way i can do that, some kind o pointer which points to n memory address without limitations.

i dont know if i made me clear, but if something was not clear i can try to explain again. thanks :)

Last edited on
WHY do you want to create such an entity? Are you sure it is what you want? What is its purpose and how do you intend to index it afterwards?
it's just a sketch for a math problem, i don't want to get into details. But yeah will be cool if i could do that way. I have sure that i cant handle this problem with another way, but i really want to try in this type of data structure.
What maths problem?
sorry, but i really don't want get to details. Just want to know if it's possible to do this kind of thing. And if it is, how to do so. But thanks for the answers anyway :)
Well, if n is input at run time then you would probably have to use a one-dimensional array (of dimension n!) and form the equivalent 1-d index from [i][j][k]...

If you have to do that then I'm not sure it would be a good method of solving your mysterious maths problem.

Note also that factorials grow very quickly.
i thouth that. If i really have to that way will for sure be a bad solution. But thanks anyway. It's not a mysterious problem, is just a -not so complicated- problem that i stuck in. I kind feel ashame that i could't solve by myself, thats why i dont say. haha. hope you understand. :)
I don't think it's a bad solution. It's more or less how it has to be done. You could of course wrap it up into some nice class, or use an existing library if you can find one, but it doesn't change what's really going on under the hood.
If it's a bad solution, then it would be a bad solution either way.

The only difference between what lastchance is suggesting and the [x][y][z][w][...] way of doing it is syntactic sugar. The former might not be possible dynamically (at least easily), but the latter could be possible by using variadic functions like auto n = element_at(x, y, z, w, ...);, or passing in an std::vector to represent each dimension element_at( {x, y, z, w, ... } );

But you have to map the higher-dimensions to a single dimension (this is what obviously has to be done at a 1-dimensional memory level regardless of any syntactic sugar, whether we're dealing with a 2D array or an 11D array).
Last edited on
I guess you are never going to tell us what your maths problem is, @zigooo, but if it is something like Pascal's triangle, or binomial coefficients, then that isn't an n! dimensional matrix: it's a two-dimensional matrix (with ragged edges) and you'd use a vector<vector<int>>. If that isn't your problem then you can ignore this post.
@zigzoo, you are confused as to what a dimension is. If you really wanted these to be dimensions then you would realize that a dimension of size 1 doesn't really make sense. It's far more likely that what you want is a ragged array like lastchance said.

That's why we need to know something about your problem. For instance, if you wanted to be able to store something like this:

0
1 2
3 4 5
6 7 8 9


Then that is just a two-dimensional array with a ragged size for the second dimension. It's not an array with 4 dimensions.
@zigooo:
Are you familiar with XY problem? https://en.wikipedia.org/wiki/XY_problem

This thread looks like one.
The answer is, you can't do what you want to do the way you want to do it.

in c++ code, via pointers, you can certainly say

double ***x;
but that can't then be increased to
double ******x;
because its TYPE is known at compile time, and its type is governed by the number of *s on it. double* and double** are two different types.

the same is true for arrays and vectors:
you can say
double x[1][2];
but you can't change that at run time to be
double x[1][2][3];

and you can't change vector<vector<double> > x to have another vector wrapper around it.

the best answer is to just do vector<double> x (5+3+2+1) //or multiply them if you wanted 5!
or you can use a map such that the layer, layer instance, and data are tied together. something that ends up (logically) being x(5,0,3.14) for 5th dimension, first element = 3.14 or the like. Maps are less efficient for iterative computational work, though, so the vector and a function to index it the way you want it is probably better, assuming that it being a mysterious math problem you probably need to iterate the thing.

Last edited on
@jonnin, thanks for the answer. it clarify somethings about the topic.
@keskiverto i don't, but i'll read about.
@tpb, i guess not, let put that way, imagine if you have a 5d cube, than in each vertex you want to have a 4d cube and each vertex of those 4d's you have a 3d's and so on.
@Ganado i did a kind of similar structure, but didn't work well, i have to do more test and see if this sketch really solve the problem and put it on my article. But thanks anyway.

Thanks everyone, i put the topic as solved. Cheers and good coding for everybody :)
Last edited on
A vertex a 4d cube is a 3d cube?
A vertex of a 3d cube is not a 2d cube, it's still a point, albeit a 3d point. Anyway good luck.
@Ganado ops, my mistake. Its the dimension, if you have (a,b,c,d), them a stores (a1,a2,a3), b stores (b1,b2,b3)... and each a-i and b-i store another (n-1) dimension. Something like that. Its where came the idea of matrix[n][n-1][n-2]...[n-i]. but @jonnin already say that its not possible in the way that i first think.

ps: the cube was a bad example.
Last edited on
Topic archived. No new replies allowed.