1 #ifndef STAN_MATH_PRIM_FUN_ONE_HOT_ARRAY_HPP
2 #define STAN_MATH_PRIM_FUN_ONE_HOT_ARRAY_HPP
3
4 #include <stan/math/prim/err.hpp>
5 #include <vector>
6
7 namespace stan {
8 namespace math {
9
10 /**
11 * Return an array with 1 in the k-th position and zero elsewhere.
12 *
13 * @param K size of the array
14 * @param k position of the 1 (indexing from 1)
15 * @return An array of size K with all elements initialised to zero
16 * and a 1 in the k-th position.
17 * @throw std::domain_error if K is not positive, or if k is less than 1 or
18 * greater than K.
19 */
one_hot_array(int K,int k)20 inline std::vector<double> one_hot_array(int K, int k) {
21 static const char* function = "one_hot_array";
22 check_positive(function, "size", K);
23 check_bounded(function, "k", k, 1, K);
24
25 std::vector<double> v(K, 0);
26 v[k - 1] = 1;
27 return v;
28 }
29
30 } // namespace math
31 } // namespace stan
32
33 #endif
34