how to randomly assign values from a vector and store it in another vector?

2 views (last 30 days)
good night all.
I wrote this algorithm to assign values from one vector (detected) and store in another vector (alocados), but it does it in an ordered way one by one.
How can I modify this algorithm so that it selects the values of the vector (detected) randomly, and stores them in the vector (alocados).
Any help is really appreciated.

Answers (1)

KSSV
KSSV on 18 Nov 2021
Read about randperm.
load('detected.mat')
idx = randperm(length(detected)) ;
alocados = detected(idx) ;
  1 Comment
giancarlo maldonado cardenas
Hello thanks for answering, I think I did not explain it very well.
I was looking for an answer with the same structure of my code, because there are conditions, for example here the variable prb is greater than detected and has enough space to assign the values ​​of the detected vector, in the case that prb is less than the detected vector the for would have to stop because it would no longer have space to assign the detected values.
then in each iteration of the for I would have to select a value from the vector (detected) randomly, and store in the vector (alocados) for example in my code the variable prb is 15 and in the vector detected I have 11 values ​​therefore it is They could assign the 11 values ​​because I have 15 spaces, in the case that the prb variable is 8 in the detected vector I have 11 then only 8 values ​​of the 11 can be assigned because I only have 8 spaces, and the for should stop when do not have more spaces to allocate in this specific case the for would stop in the 8th iteration.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!