|
Boost Users : |
Subject: Re: [Boost-users] Matrix initialization in uBLAS
From: Julian Gonggrijp (j.gonggrijp_at_[hidden])
Date: 2011-03-29 20:05:59
Oscar Vargas wrote:
> It's the first time I use uBLAS, and I want to know the right way to initialize a matrix.
>
> Example:
> | 3 2|
> M = | 1 5|
> | 3 4|
>
> How can I create such a matrix?
The documentation is very clear on how to create a matrix:
http://www.boost.org/doc/libs/1_46_1/libs/numeric/ublas/doc/matrix.htm#12Example
In your example, you could do at least two things:
// option 1: just assign to each cell
// somewhere inside a function...
matrix<double> M (2, 3); // or matrix<int>, matrix<complex>...
M(0, 0) = 3;
M(0, 1) = 2;
M(1, 0) = 1;
M(1, 1) = 5;
M(2, 0) = 3;
M(2, 1) = 4;
// option 2: define a constant two-dimensional array as initializer
const int ROWS = 3;
const int COLS = 2;
const int MATRIX_INIT[][COLS] = {{3, 2}, {1, 5}, {3, 4}};
// somewhere inside a function...
matrix<double> M (ROWS, COLS); // or int, complex, whatever
for (int i = 0; i < ROWS; ++i)
for (int j = 0; j < COLS; ++j)
M(i, j) = MATRIX_INIT[i][j];
Which option is best depends on your needs. There might be more
options that I'm not aware of but this should get you started.
HTH,
Julian
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net