MATLAB: Insert into array with probability

arraygeenrate eventprobability

I have one array a=[…] and confidence array. Confidence array have the same length with a array, and confidence(i) means the probability with wich i should insert element a(i) into array b. How can i generate inserting into b array with probability?
so for example if a(1)=5 and confidence(1)=0.37 it means that i have to insert 5 into array b with probability 0.37

Best Answer

% Make some arbitrary a, b, and confidence matrix
a = randi(10,[1 10]);
b = 100*(1:numel(a));
confidence = rand(size(a));
% Replace the elements of b based on the probabilities in "confidence"
pos = (confidence >= rand);
b(pos) = a(pos)