-
for loops
Hi all,
Can anyone help me with the following problem:
I want to cycle through a multidimensional matrix M.
Suppose this matrix has dimension 2 with boundaries aa and bb, then I could use something like this:
Code:
for(a=0; a<aa; a++)
{
for(b=0; b<bb; b++)
{
M[a][b] = ...;
}
}
But now, suppose that at compile time I don't now the dimensions nor the boundaries of matrix M!
I want to write some kind of general code that can cope with all possible dimensions of the matrix. So, if the matrix has dimension three, the code will automatically add a third 'for' loop.
Is this possible or should I do this completely different?
Thanks in advance!
Bropolwig
-
In C++, is it possible to define an array with unknown dimensions (C99?)?
-
How do you define your matrix?
The recursive approach is most simple to produce:
Code:
func(matrix):
if matrix is an array:
for (i = 1 to len(matrix)) do func(matrix(i))
else
it's a scalar number, process it
end func
-
Well to allow run-time choice of aa, bb you would do something like this
Code:
int **M;
M = new int*[aa]
for ( int i = 0 ; i < aa ; i++ ) M[i] = new int[bb]
There isn't a good way of varying the number of dimensions at run-time, since the root type (int ** in this case) has to be fixed at compile time.
I suppose you could code each matrix element as a list, which can contain other lists to inner dimensions of the matrix (and so on recursively), but you'd lose the easy and quick subscripted access method doing so.
-
Using dynamic memory allocation I can indeed choose aa and bb at runtime.
The idea behind the undefined dimensions of the matrix is that I have an undefined parameterspace at start. When the program executes the user enters the number of parameters. Each matrix dimension represents a parameter and I want to evaluate the whole parameterspace by cycling through all dimensions of the matrix.
Maybe I can do this smarter? :confused:
Thanks
-
Possibly. What are you trying to implement?
-
Oh, it is just a hobby project of mine :)
For this specific part, I just want to be able to store an outcome (int value) for every combination of parameters, given that the number and range of parameters is set at run-time.
Thanks,
Bropolwig
-
I meant, why do you go through those values? To find the optimum in some respect, to calculate a statistic, something else? There are better algorithms than a brute-force search.
Also, let's say you have 10 parameters each of which can take 100 values which is pretty modest. Then the entire search space contains 100^10 values which is a mighty big number. You can't really loop over that. Not to mention the amount of memory storing the matrix would require.
-
Indeed, this is what I want to do: find an optimum.
But even if there are smarter ways of searching instead of looping through all number, then I still need to store them first, right?
Could you point me to some of those smarter algorithms joni?
Thanks a lot for the help,
Bropolwig
-
No, you don't need to store the values. You just need to have a way to calculate them, which you do seem to have.
The technique you would use depends on the nature of the problem, such as if there are many optimums or only a unique one, or whether you are looking for local or global optimums. I'm no expert in optimization but I've heard evolutionary algorithms have often been used with good results when the parameter space has a big dimension. There's a pretty good list of different techniques at wikipedia:
http://en.wikipedia.org/wiki/Optimiz...%29#Techniques
-
So you're saying a and b can be very high, but you dont know just how?
One way is, use a single dimension array that can expand to your whim.
Code:
#include <vector>
#include <iostream>
using namespace std;
int main() {
vector <int> matrix; //Creates a vector with an expandable size.
unsigned int x;
matrix.push_back(10); //Adds an element to the vector, with a value of 10.
matrix.push_back(5); //Adds another element with a value of 5.
for(x=0; x < matrix.size(); x++)
cout << matrix[x]; //Couts the integer value of matrix[x].
matrix.pop_back(); //Deletes the last added element of the vector.
}
Your other option (WARNING! DO NOT RUN THIS CODE!)
You might be able to use a 2d vector, but I dont know. Wait...
Code:
vector <int> MatrixPartOne;
vector <int> MatrixPartTwo;
Their :P. The only problem is its two seperate entities. If you want more on vectors, http://www.cppreference.com/cppvector/ .
If I didint answer your question, I blame vectors for intoxicating me :).
-
Well, the real problem, Blackroot, is that I don't know at compile time whether or not there is another dimension c, or d, or etc... of the matrix.