source: mmcs/armadillo_bits/glue_histc_meat.hpp @ 8daa049

matrices
Last change on this file since 8daa049 was 9dd61b1, checked in by rboet <rboet@…>, 9 years ago

Avance del proyecto 60%

  • Property mode set to 100644
File size: 4.2 KB
Line 
1// Copyright (C) 2012-2015 Conrad Sanderson
2// Copyright (C) 2012-2015 NICTA (www.nicta.com.au)
3// Copyright (C) 2012 Boris Sabanin
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
9
10
11template<typename T1, typename T2>
12inline
13void
14glue_histc::apply(Mat<uword>& out, const mtGlue<uword,T1,T2,glue_histc>& in)
15  {
16  arma_extra_debug_sigprint();
17 
18  typedef typename T1::elem_type eT;
19 
20  const uword dim = in.aux_uword;
21 
22  const unwrap_check_mixed<T1> tmp1(in.A, out);
23  const unwrap_check_mixed<T2> tmp2(in.B, out);
24 
25  const Mat<eT>& X = tmp1.M;
26  const Mat<eT>& E = tmp2.M;
27 
28  arma_debug_check
29    (
30    ((E.is_vec() == false) && (E.is_empty() == false)),
31    "histc(): parameter 'edges' must be a vector"
32    );
33 
34  arma_debug_check
35    (
36    (dim > 1),
37    "histc(): parameter 'dim' must be 0 or 1"
38    );
39 
40  const uword X_n_elem = X.n_elem;
41  const uword X_n_rows = X.n_rows;
42  const uword X_n_cols = X.n_cols;
43 
44  const uword E_n_elem = E.n_elem;
45 
46  if( E_n_elem == 0 )
47    {
48    out.reset();
49    return;
50    }
51 
52 
53  // for vectors we are currently ignoring the "dim" parameter
54 
55  uword out_n_rows = 0;
56  uword out_n_cols = 0;
57 
58  if( (X.vec_state == 0) && (X.n_elem == 1u) )
59    {
60    if(out.vec_state == 1u)
61      {
62      out_n_rows = E_n_elem;
63      out_n_cols = 1;
64      }
65    else
66      {
67      out_n_rows = 1;
68      out_n_cols = E_n_elem;
69      }
70    }
71  else
72  if( (X.vec_state > 0) || X.is_vec() )
73    {
74    if(X.vec_state == 2u)
75      {
76      out_n_rows = 1;
77      out_n_cols = E_n_elem;
78      }
79    else
80    if(X.vec_state == 1u)
81      {
82      out_n_rows = E_n_elem;
83      out_n_cols = 1;
84      }
85    else
86    if(X.is_rowvec())
87      {
88      out_n_rows = 1;
89      out_n_cols = E_n_elem;
90      }
91    else
92    if(X.is_colvec())
93      {
94      out_n_rows = E_n_elem;
95      out_n_cols = 1;
96      }
97    }
98  else
99    {
100    if(dim == 0)
101      {
102      out_n_rows = E_n_elem;
103      out_n_cols = X_n_cols;
104      }
105    else
106    if(dim == 1)
107      {
108      out_n_rows = X_n_rows;
109      out_n_cols = E_n_elem;
110      }
111    }
112 
113  out.zeros(out_n_rows, out_n_cols);
114 
115  const eT* E_mem = E.memptr();
116
117  if( (X.vec_state > 0) || X.is_vec() )
118    {
119          uword* out_mem = out.memptr();
120    const eT*    X_mem   = X.memptr();
121   
122    for(uword j=0; j<X_n_elem; ++j)
123      {
124      const eT val = X_mem[j];
125     
126      for(uword i=0; i<E_n_elem-1; ++i)
127        {
128        if( (E_mem[i] <= val) && (val < E_mem[i+1]) )
129          {
130          out_mem[i]++;
131          break;
132          }
133        else
134        if(val == E_mem[E_n_elem-1])
135          {
136          // in general, the above == operation doesn't make sense for floating point values (due to precision issues),
137          // but is included for compatibility with Matlab and Octave.
138          // Matlab folks must have been smoking something strong.
139          out_mem[E_n_elem-1]++;
140          break;
141          }
142        }
143      }
144    }
145  else
146  if(dim == 0)
147    {
148    for(uword col=0; col<X_n_cols; ++col)
149      {
150            uword* out_coldata = out.colptr(col);
151      const eT*    X_coldata   = X.colptr(col);
152     
153      for(uword row=0; row<X_n_rows; ++row)
154        {
155        const eT val = X_coldata[row];
156       
157        for(uword i=0; i<E_n_elem-1; ++i)
158          {
159          if( (E_mem[i] <= val) && (val < E_mem[i+1]) )
160            {
161            out_coldata[i]++;
162            break;
163            }
164          else
165          if(val == E_mem[E_n_elem-1])
166            {
167            out_coldata[E_n_elem-1]++;
168            break;
169            }
170          }
171        }
172      }
173    }
174  else
175  if(dim == 1)
176    {
177    for(uword row=0; row<X_n_rows; ++row)
178      {
179      for(uword col=0; col<X_n_cols; ++col)
180        {
181        const eT val = X.at(row,col);
182       
183        for(uword i=0; i<E_n_elem-1; ++i)
184          {
185          if( (E_mem[i] <= val) && (val < E_mem[i+1]) )
186            {
187            out.at(row,i)++;
188            break;
189            }
190          else
191          if(val == E_mem[E_n_elem-1])
192            {
193            out.at(row,E_n_elem-1)++;
194            break;
195            }
196          }
197        }
198      }
199    }
200  }
Note: See TracBrowser for help on using the repository browser.