Fn_find

Functions

template<typename eT >
arma_hot u32 find_helper (u32 *out_mem, const eT *in_mem, const u32 n_elem)
template<typename T1 >
Mat< u32find (const Base< typename T1::elem_type, T1 > &X, const u32 k=0, const char *direction="first")

Function Documentation

template<typename eT >
arma_hot u32 find_helper ( u32 out_mem,
const eT *  in_mem,
const u32  n_elem 
) [inline]

Definition at line 27 of file fn_find.hpp.

Referenced by find().

00028   {
00029   arma_extra_debug_sigprint();
00030   
00031   u32 n_nz = 0;
00032   
00033   for(u32 i=0; i<n_elem; ++i)
00034     {
00035     if(in_mem[i] != eT(0))
00036       {
00037       out_mem[n_nz] = i;
00038       ++n_nz;
00039       }
00040     }
00041    
00042   return n_nz;
00043   }

template<typename T1 >
Mat<u32> find ( const Base< typename T1::elem_type, T1 > &  X,
const u32  k = 0,
const char *  direction = "first" 
) [inline]

Definition at line 52 of file fn_find.hpp.

References arma_stop(), find_helper(), Base< elem_type, derived >::get_ref(), Mat< eT >::memptr(), and Mat< eT >::rows().

00053   {
00054   arma_extra_debug_sigprint();
00055   
00056   typedef typename T1::elem_type eT;
00057   
00058   const unwrap<T1>   tmp(X.get_ref());
00059   const Mat<eT>& A = tmp.M;
00060   
00061   const u32 n_elem = A.n_elem;
00062   
00063   Mat<u32> indices(n_elem, 1);
00064   
00065   const u32 n_nz = find_helper(indices.memptr(), A.memptr(), n_elem);
00066   
00067   const char sig = direction[0];
00068   
00069   // return the indices of all n_nz elements, otherwise return an empty matrix
00070   if(n_nz > 0)
00071     {
00072     if(sig == 'f' || sig == 'F')   // "first"
00073       {
00074       return ( (k > 0 && k <= n_nz) ? indices.rows(0,      k-1   ) : indices.rows(0, n_nz-1) );
00075       }
00076     else
00077     if(sig == 'l' || sig == 'L')   // "last"
00078       {
00079       return ( (k > 0 && k <= n_nz) ? indices.rows(n_nz-k, n_nz-1) : indices.rows(0, n_nz-1) );
00080       }
00081     else
00082       {
00083       arma_stop("find(): 3rd input argument must be \"first\" or \"last\"");
00084       
00085       return Mat<u32>();
00086       }
00087     }
00088   else
00089     {
00090     return Mat<u32>();
00091     }
00092   }