Random combinations of symbols (restored)

1 Ansicht (letzte 30 Tage)
Trevor Badji
Trevor Badji am 18 Dez. 2017
Bearbeitet: Matt J am 22 Feb. 2018
Matt J undeleted this a 3rd time: (I won't stop)
probVector = [0.4 0.05 0.2 0.15 0.2];
P(A)= 0.4 P(B)=0.05 P(C)=0.2 P(D)=0.15 P(E)=0.2
function b = GenerateInputSymbols(N, probVector)
%This function generates a random vector of N characters according the probability mass function specified in probVector.
%Each character is generated independently of all the others.
%Use the rand function (or functions related to rand in MATLAB) in order generate a realization of such a random vector of N characters.
end
For Example;
N=3;
Output= [0.05 0.15 0.4]
How we can generate this function? We want that from probvectors values and porbability of Letters A B C D E , as a result of this function we want to see AEBCD or BDCEA... randomly one of them. PS: It has to return values as related with their probabilities. For example in 100 trials there would 40 A's 5 B's 20 C'S ....
  8 Kommentare
Guillaume
Guillaume am 21 Dez. 2017
"I have all the copyrights of this question and do not give permission to edit"
As per the terms of use you agreed to when you created you account: You agree that all Content that you contribute to the MATLAB Answers portion of MATLAB Central will be licensed under the Creative Commons Attribution Share Alike 3.0 license. Which means that you agreed that any post could be edited and you cannot revoke this permission.
Jan
Jan am 26 Dez. 2017
Thanks Guillaume and Matt J for caring about this thread.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jan
Jan am 18 Dez. 2017
Bearbeitet: Jan am 18 Dez. 2017
If you have the symbols and probabilities
symbols = 'ABCDE'
prob = [0.4, 0.05, 0.2, 0.15, 0.2];
You can get one symbol with its probability using:
probAccum = cumsum(prob);
probAccum(end) = 1.0; % Care for rounding errors
index = find(rand >= probAccum, 1);
Now the chance to get index=2 is 4 times higher than index=3.
I leave the implementation for multiple N and creating the function up to you, because this looks like a homework question.
See histcounts for another approach.

Jos (10584)
Jos (10584) am 21 Dez. 2017
A perfect job for my old RANDP function that picks random values using probabilities :)
Objects = 'ABCDEF'
probVector = [0.4 0.05 0.2 0.15 0.2];
N = 10 ;
idx = randp(probVector, 1, N)
List = Objects(idx)
RANDP can be downloaded from the File Exchange: https://uk.mathworks.com/matlabcentral/fileexchange/8891-randp

Kategorien

Mehr zu Language Support finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by