Borrar filtros
Borrar filtros

selecting a random element from vector and ... in matlab

1 visualización (últimos 30 días)
Jack Ie
Jack Ie el 2 de Abr. de 2016
Comentada: Walter Roberson el 5 de Abr. de 2016
I have 3 problem in matlab:
  1. selecting a random element from vector with probability proportional to the occurrence frequency of this element in the vector?
  2. selecting the most popular element from vector(the most occurrence frequency of this element in the vector)?
  3. converting a vector into a probability distribution of elements.If the probability of seeing a particular element is less than a given threshold r=0.5, this element is deleted from vector.
Thanks a lot.

Respuesta aceptada

Walter Roberson
Walter Roberson el 2 de Abr. de 2016
For the first one see http://www.mathworks.com/help/stats/randsample.html . Just submit the vector as the population, and since the population will be chosen from uniformly randomly, the probability that any particular value will be chosen will depend upon how frequently it occurs relative to the other values.
For the third one:
[uniquepop, ~, popidx] = unique(ThePopulation);
counts = histc(popidx, 1:length(uniquepop));
selected_elements = uniquepop( find(counts ./ length(ThePopulation) >= r ) );
Note: with r = 0.5, selected_elements may have 0, 1, or 2 entries.
  2 comentarios
Jack Ie
Jack Ie el 3 de Abr. de 2016
Thanks a lot Walter. for the first problem when vector has one element, result of randsample is not correct. it return random number from 1 to that element. for example: n=[3]; x = randsample(n,1); x=1 or x=2 or x=3 but I want x=3
Walter Roberson
Walter Roberson el 5 de Abr. de 2016
In the case that you have a population that has only one element, then use two copies of the element.
n = [3 3];
x = randsample(n, 1);

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by