1 #ifndef STAN_MATH_PRIM_FUN_ONE_HOT_ROW_VECTOR_HPP 2 #define STAN_MATH_PRIM_FUN_ONE_HOT_ROW_VECTOR_HPP 3 4 #include <stan/math/prim/err.hpp> 5 #include <stan/math/prim/fun/Eigen.hpp> 6 7 namespace stan { 8 namespace math { 9 10 /** 11 * Return a row vector with 1 in the k-th position and zero elsewhere 12 * 13 * @param K size of the row vector 14 * @param k position of the 1 (indexing from 1) 15 * @return A row vector 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_row_vector(int K,int k)20inline Eigen::RowVectorXd one_hot_row_vector(int K, int k) { 21 static const char* function = "one_hot_row_vector"; 22 check_positive(function, "size", K); 23 check_bounded(function, "k", k, 1, K); 24 25 Eigen::RowVectorXd ret = Eigen::RowVectorXd::Zero(K); 26 ret(k - 1) = 1; 27 return ret; 28 } 29 30 } // namespace math 31 } // namespace stan 32 33 #endif 34