- Joined
- Dec 2, 2008
- Messages
- 9
- Reaction score
- 0
Hey guys,
I was wondering how could I implement a 4d Matrix/Array that will have different sizes for the 4th dimension. For example the element at (3,4,5) will have a 4th dimension that has a size of 3, while element(1,2,3) will have a 4th dimension of 6. On this forum I found this code written by Daniel T. where he builds a matrix of 4 dimensions.
Initially I was thinking I could solve the problem through an array, but i am not sure if a 4D array will allow me to have different lengths for the 4th D, so now I am starting to think of a Matrix, however I haven't used them too much, so I am not sure how I could modify Daniel's code to fit my needs. Any help would be greatly appreciated.
Cheers
I was wondering how could I implement a 4d Matrix/Array that will have different sizes for the 4th dimension. For example the element at (3,4,5) will have a 4th dimension that has a size of 3, while element(1,2,3) will have a 4th dimension of 6. On this forum I found this code written by Daniel T. where he builds a matrix of 4 dimensions.
Code:
class Matrix4
{
std::vector<double> storage;
int bb;
int cc;
int dd;
public:
Matrix4( int a, int b, int c, int d ):
storage( a*b*c*d ), bb( b ), cc( c ), dd( d ) { }
double& operator()( int a, int b, int c, int d ) {
return storage.at( bb*cc*dd*a + cc*dd*b + dd*c + d );
}
};
You use it like this:
Matrix4 m( 1, 2, 3, 5 );
m( 0, 1, 0, 4 ) = 5.0;
assert( m( 0, 1, 0, 4 ) == 5.0 );
Initially I was thinking I could solve the problem through an array, but i am not sure if a 4D array will allow me to have different lengths for the 4th D, so now I am starting to think of a Matrix, however I haven't used them too much, so I am not sure how I could modify Daniel's code to fit my needs. Any help would be greatly appreciated.
Cheers