Yudan Yi wrote:

I have a problem to copy (assign) a matrix to another matrix. Curreny, I

know copy the number using loops, while it will take some time, I wonder

if there have faster method. The following code explain my situation

detailed.

double ** matrixa, **matrixb;

int nrow = 10, mcol = 10;

matrixa = initmatrix(nrow, mcol); // allocate memory a

matrixb = initmatrix(nrow, mcol); // allocate memory b

// copy a => b

for (int i=0;i<nrow;i++)

for (int j=0;j<mcol;j++)

matrixb[i][j] = matrixa[i][j];

freematrix(matrixa, nrow, mcol); // free memory

freematrix(matrixb, nrwo, mcol); // free memory

I want to find another way to copy the data from a => b without loop all

the data.

Thanks

For matrices, you are better off by far using a single array and addressing

it as matrix[ncols * i + j]. You get better data locality in the cache,

and you don't need a loop of malloc/new every time you want to make a copy.

With modern compilers & processors, the cost of the multiply is usually

either optimized away completely or irrelevant (a multiply is a cycle or

so, a memory lookup is at least a few, more likely a few dozen, cycles).

If you use something like BLAS (

http://www.netlib.org/blas) to do the

underlying arithmetic, then even this cost disappears. Versus using nested

arrays requires an additional indirection which may be quite expensive.

Try wrapping this in a class as well, eg

class Matrix

{

public:

Matrix() : nrows_(0), ncols_(0), Data_(0) {}

Matrix(int nrows, int ncols) : nrows_(nrows), ncols_(ncols),

Data_(new double[nrows_ * ncols_]) {}

Matrix(Matrix const& Other) // fill in boring details

Matrix& operator=(Matrix const& Other); // fill in boring details

~Matrix() { delete[] Data_; }

double operator()(int i, int j) { return Data_[i * ncols + j]; }

// plus some functions to get the size, etc

private:

int nrows_, ncols_;

double* Data_;

};

With a bit of thought, you can do operator[] too, so you can use the 'old'

notation. Hint: return a pointer to the start of the row of the matrix.

But the operator() solution is easier, especially becuase it is trivial to

add some debug assert() statements to get bounds checking. That can

potentially save you an unbelievable amount of time, as can wrapping all of

the ugly memory management stuff inside a class.

By the way, for builtin types like double, you can use memcpy() to implement

the copy constructor and copy-assignment. Another advantage of the

single-array approach: you only need a single memcpy, versus a loop of them

for the multi-array approach. But don't do this for arrays of user-defined

types!

HTH,

Ian McCulloch