Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project!

## Re: [Vxl-users] (no subject)

 Re: [Vxl-users] (no subject) From: Amitha Perera - 2003-02-03 17:15:47 ```On Mon 03 Feb 2003, Bettini wrote: > Hi users, > > does anyone know, or has anyone realized a function-operator to make the > same element-by-element 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 result = element_product( A, B ); would do what you want. > I used a function-operator based on two for-cicles: > > inline void mul_dot(vnl_matrix &A, vnl_matrix &B, > vnl_matrix &temp,const int &dim){ > unsigned int j; > for(unsigned int i=0; i for(j=0; 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 to--and shouldn't--pass 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 const& A, vnl_matrix const& B, vnl_matrix & 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. ```