Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.44 KB | None | 0 0
  1. library(Rcpp)
  2.  
  3. cppFunction('
  4. NumericMatrix imembrandc(int n, int k) {
  5. NumericMatrix u( n , k );
  6. IntegerVector sequ = seq(1,100);
  7. NumericVector sampled;
  8. for (int i=0; i < k; ++i) {
  9. sampled = sample(sequ, n);
  10. u(_,i) = sampled / sum(sampled);
  11. }
  12.  
  13. if (is_true(any(rowSums(u) != 1))) {
  14. u(_,1) = u(_,1) + (1 - rowSums(u));
  15. }
  16.  
  17. return(u);
  18. }')
  19.  
  20. res = imembrandc(n = 10, k = 5)
  21. rowSums(res)
  22.  
  23. rowSums(res) == 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement