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

3 次查看(过去 30 天)
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.

回答(1 个)

KSSV
KSSV 2021-11-18
Read about randperm.
load('detected.mat')
idx = randperm(length(detected)) ;
alocados = detected(idx) ;
  1 个评论
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 LTE Toolbox 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by