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

3 ビュー (過去 30 日間)
giancarlo maldonado cardenas
giancarlo maldonado cardenas 2021 年 11 月 18 日
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
giancarlo maldonado cardenas 2021 年 11 月 18 日
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 Center および File ExchangeLTE Toolbox についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by