insert into array with probability
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Teja Muppirala
on 3 Jun 2011
% 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)
0 Comments
More Answers (1)
Oleg Komarov
on 3 Jun 2011
% Create sample a and cf
a = randi([1,100],4,1);
cf = rand(4,1);
cf = cf/sum(cf);
% Suppose our b has 1000 elements
n = 1000;
% create b
len = round(n*cf);
b = rude(len,cf); % You can find rude on the fex
% Make sure b has 1000 elements
numB = numel(b);
if numB > n
b(randi([1 n],1)) = 0;
elseif numB < n
b = [b a(randi([1 4],1))];
end
% Scatter b
b = b(randperm(n));
0 Comments
See Also
Categories
Find more on Resizing and Reshaping Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!