-1

i have 4 vectors:

x1 = [-27.445,-26.960,-26.635,-26.431,-26.315];
x2 = [-25.673,-25.497,-25.449,-25.491,-25.593];
x3 = [-29.310,-28.201,-27.240,-26.399,-25.654];
x4 = [-28.761,-27.103,-26.290,-25.605,-25.025];

i have to generate 100 vectors randomly, but according to the probability that x1 has 40% weight, x2 has 30%, x3 has 20% and x4 has 10%.

Can somebody help me how to do this in matlab? Thanks in advance.

  • 2
    you want to generate values that are within `x1`, `x2`, `x3` and `x4`? Generating 100 vectors is a bad idea. You should create a single matrix containing all of them – Sardar Usama Mar 16 '18 at 11:22
  • this new vectors, they need to be combinations of those 4 or copies of those 4? Is this post a duplicate? https://stackoverflow.com/questions/2977497/weighted-random-numbers-in-matlab – Ander Biguri Mar 16 '18 at 11:33
  • To clear my question above: every vector (x1~x4) corresponds to 1 transfer function. My goal it to generate those transfer functions in ramdom but with weights. .. thank you very much.. lastly, this is not a duplicate post. – Lovelyn Garcia Mar 17 '18 at 04:04

1 Answers1

0

This is how you do it:

N=100;
W0=[40,30,20,10]'; %column vec

x1 = [-27.445,-26.960,-26.635,-26.431,-26.315];
x2 = [-25.673,-25.497,-25.449,-25.491,-25.593];
x3 = [-29.310,-28.201,-27.240,-26.399,-25.654];
x4 = [-28.761,-27.103,-26.290,-25.605,-25.025];
X0=cat(1,x1,x2,x3,x4);

assert(size(W0,1)==size(X0,1)) %ensure same size of W and X 

W=W0/sum(W0); %normalize w to be probability vector
S=cumsum(W); % prepare the trick. we going to hit with rand() in range with proability

Rands=rand(1,N);
IsInRange=Rands<S; %for old matlab use: IsInRange=bsxfun(@lt,Rands,S)
[~,I]=max(IsInRange); %find the index

Result=X0(I,:)
Mendi Barel
  • 3,350
  • 1
  • 23
  • 24
  • thanks Mendi Barel.. can i have a follow up question? If i have x1 up to xn vectors, and i want to generate a random vector with weights, x1 having the highest probability, does the above algorithm will still work? thanks in advance, just having a hard time on thesis – Lovelyn Garcia Mar 17 '18 at 08:18
  • of course. just make sure that for every vector u have weight. and concat them all to single matrix with X0=cat(1,x1,x2,....xn); – Mendi Barel Mar 17 '18 at 09:19