Hi everyone,
First of all, sorry my english (I am spanish and I don't know writing english very well).
I have a doubt about how to go through a multidimensional matrix generically.
Examples of how to go through in particular cases:
-> 1 dimension:
1 2 3 4 5
|
unsigned int d0 = 3;
double m[d0];
for (unsigned int i = 0; i < d0; i++){
m[i] = 0;
}
|
-> 2 dimensions:
1 2 3 4 5 6 7 8
|
unsigned int d0 = 3;
unsigned int d1 = 4;
double m[d0][d1];
for (unsigned int i = 0; i < d0; i++){
for (unsigned int j = 0; j < d1; j++){
m[i][j] = 0;
}
}
|
-> 3 dimensions:
1 2 3 4 5 6 7 8 9 10 11
|
unsigned int d0 = 3;
unsigned int d1 = 4;
unsigned int d2 = 5;
double m[d0][d1][d2];
for (unsigned int i = 0; i < d0; i++){
for (unsigned int j = 0; j < d1; j++){
for (unsigned int k = 0; k < d2; k++){
m[i][j][k] = 0;
}
}
}
|
-> 4 dimensions:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
unsigned int d0 = 3;
unsigned int d1 = 4;
unsigned int d2 = 5;
unsigned int d3 = 6;
double m[d0][d1][d2][d3];
for (unsigned int i = 0; i < d0; i++){
for (unsigned int j = 0; j < d1; j++){
for (unsigned int k = 0; k < d2; k++){
for (unsigned int l = 0; l < d3; l++){
m[i][j][k][l] = 0;
}
}
}
}
|
How you can see, it is necesary so many for loops as matrix's dimensions.
I want to do an algorithm that go through a multidimensional matrix generically, that is, knowing the number of dimensions and the dimensions of one multidimensional matrix, go through all its elements. For example, it would be to implement this function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
/**
* Go through the multidimensional matrix given, assigning zero to all its
* elements
* @param dimensionsNumber The number of dimensions of the multidimensional
* matrix
* @param dimensions A pointer to the first element of one array of
* unsigned ints who size is the same as the number of dimensions given, so
* the ith element is the ith dimension of the multidimensional matrix
* @param multidimensionalMatrix A pointer to the first element of the
* multidimensional matrix
*/
void assignZeros(unsigned int dimensionsNumber,
unsigned int* dimensions,
double* multidimensionalMatrix){
// How to go through the multidimensional matrix?
}
|
This function must be used thus, for example:
1 2 3 4 5 6
|
// 1 dimension //
unsigned int dimensionsNumber = 1;
unsigned int dimensions[1] = {3};
double multidimensionalMatrix[3];
assignZeros(dimensionsNumber, dimensions, multidimensionalMatrix);
|
1 2 3 4 5 6 7
|
// 2 dimensions //
unsigned int dimensionsNumber = 2;
unsigned int dimensions[2] = {3, 4};
double multidimensionalMatrix[3][4];
assignZeros(dimensionsNumber, dimensions,
reinterpret_cast<double*>(multidimensionalMatrix));
|
1 2 3 4 5 6 7
|
// 3 dimensions //
unsigned int dimensionsNumber = 3;
unsigned int dimensions[3] = {3, 4, 5};
double multidimensionalMatrix[3][4][5];
assignZeros(dimensionsNumber, dimensions,
reinterpret_cast<double*>(multidimensionalMatrix));
|
1 2 3 4 5 6 7
|
// 4 dimensions //
unsigned int dimensionsNumber = 4;
unsigned int dimensions[4] = {3, 4, 5, 6};
double multidimensionalMatrix[3][4][5][6];
assignZeros(dimensionsNumber, dimensions,
reinterpret_cast<double*>(multidimensionalMatrix));
|
Some idea about how implement the function assignZeros?
Thanks.