Borrar filtros
Borrar filtros

Comparing randsample and binning uniform random

1 visualización (últimos 30 días)
yeungor
yeungor el 23 de Jul. de 2018
Comentada: yeungor el 23 de Jul. de 2018
I can produce randomly polled (with replacement) values from vector x with probabilities in vector p using randsample with replacement.
X = randsample(N,K,true,W)
I can equivalently do this by using rand and histcounts (specifying edges) that correspond to the values from a cumulative sum on vector p (assuming p is normalized).
edges = [0 cumsum(p)]; edges(end)=1; % for possible rounding error
Y = rand(1,nsamples);
[~,~,bin] = histcounts(Y, edges);
X = x(bin);
My question is: if one of these is better, which one is?
  2 comentarios
Jan
Jan el 23 de Jul. de 2018
randsample is nicer, without doubt.
yeungor
yeungor el 23 de Jul. de 2018
Thanks, it's more readable, that's for sure.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by