Randsample with matrix: extract multiple values from every column of a matrix without loop!
Show older comments
I have a matrix of weights pV = rand(N,F),
e.g. pV= [0.5522 0.3922 0.0221 1 0;...
0.0947 0.4357 0.0000 0 0.0064;...
0.0214 0.0000 0.0062 0 0;...
0.3317 0.1720 0.9717 0 0.9936];
For F times, I want to extract z_k numbers from a vector 1:N, using weights taken from the matrix pV. The loop version of the code is:
F = 5;
N = 4;
z_k = 2;
for f=1:F
seller(f,:)=randsample(1:N,z_k,'true',pV(:,f));
end
I am looking for solutions without the loop to improve efficiency. I have found the following solution (pV is already normalised to 1 by column, i.e. sum(pV) = 1 1 1 1 1) but I do not know how to fill the final matrix "seller" for those columns where less than z_k numbers satisfying the condition below are found.
If not enough numbers are found for a colum, I would like to fill it first with the numbers satisfying the condition and then with random numbers.
pV= [0.5522 0.3922 0.0221 1.0000 0;...
0.0947 0.4357 0.0000 0 0.0064;...
0.0214 0.0000 0.0062 0 0;...
0.3317 0.1720 0.9717 0 0.9936];
[rand_pV_val,rand_pV_rank_idx] = sort(pV,1);
pV_cdf = cumsum(rand_pV_val,1);
%rand_pV = rand(1,size(pV,2)); Use this for reproducibility:
rand_pV = [0.1119 0.2180 0.0649 0.4878 0.7268];
seller_full = repmat(rand_pV,size(pV,1),1)<pV_cdf;
seller_zk = cumsum(seller_full,1)==1|cumsum(seller_full,1)==2 & seller_full; %alternative ways?
enough_sellers = any(cumsum(seller_zk,1)>=z_k);
seller = zeros(z_k,F);
seller(:,enough_sellers) = reshape(rand_pV_rank_idx(seller_zk(:,enough_sellers)),z_k,[]);
seller =
2 1 0 0 0
4 2 0 0 0
%How can i fill the matrix seller where I didnt find enough sellers seller(:,~ enough_sellers)?
My desired results:
seller = 2 1 4 1 4
4 2 2 3 3
where 4, 1, 4 are the rand_pV_rank_idx in the position of the ones of the seller_zk matrix; the 2, 3, 3 are just random numbers different from 4, 1, 4. It can also happen that some columns of seller_zk have only zeros.
How can I do this, filling the matrix seller(:,~ enough_sellers) as I wrote above?
Also, I'm looking for alternative ways to find the matrix seller_zk as I would like it to be flexible to different values of z_k.
Thanks
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!