From: Amitha Perera <perera@cs...>  20030203 17:15:47

On Mon 03 Feb 2003, Bettini wrote: > Hi users, > > does anyone know, or has anyone realized a functionoperator to make the > same elementbyelement operations between matrices (i.e. multiplication, > division,...), as efficient in terms of velocity of execution as in > Matlab? element_product (defined in vnl_matrix.h), so that vnl_matrix<double> result = element_product( A, B ); would do what you want. > I used a functionoperator based on two forcicles: > > inline void mul_dot(vnl_matrix<double> &A, vnl_matrix<double> &B, > vnl_matrix<double> &temp,const int &dim){ > unsigned int j; > for(unsigned int i=0; i<dim; i++){ > for(j=0; j<dim; j++) temp(i,j)=A(i,j)*B(i,j); > } > } > > but it takes more time to run than the Matlab version. Really? Do you have the optimization flags set for your compiler? A couple of comments:  You don't need toand shouldn'tpass in the dimensions of the matrix. The matrix already stores that information: A.rows() and A.columns().  Don't inline this function; the cost of the function call is negligible compared to the cost of multiplying the matrices. (Assuming they are large, as you indicate later.)  If your result matrix is already allocated and this loop is really slower that Matlab, you may want to implement something like the following (untested) void element_multiply( vnl_matrix<double> const& A, vnl_matrix<double> const& B, vnl_matrix<double> & result ) { assert( A.rows() == B.rows() && A.rows() == result.rows() ); assert( A.cols() == B.cols() && A.cols() == result.cols() ); double* pa = A.begin(); double* end = A.end(); double* pb = B.begin(); double* pr = result.begin(); for( ; pa != end; ++pa, ++pb, ++pr ) { *pr = *pa * *pb; } } > Second question: I'm working on 3D matrices (the structure is a vcl_vector > of vnl_matrix), but I have a problem of memory (stack error) in running the > algorithm when the 3D matrices have to many rows or columns (I need > 100x100x90 matrices). Does anyone know why? And does anyone know a > solution? Storing 100x100x90 requires on the order of 8MB of RAM. Unless your machine is really low on memory, this shouldn't be a problem. Perhaps you are having trouble with accessing memory outside of the matrix (i.e. indexing greater than the size of the matrix)? Or else you are copying around this vector when it may be better to pass by const reference? It is hard to diagnose. Hope this helps. Cheers, Amitha. 