hi, i have set of numbers as following :

Z=[1,5,7,4,3,6,77,88,34,73,58,97,23,55,30,49,86,76,64,64,54,54,65,76,76,62,83,59,62,57,48]; How to choose 10 random numbers from these numbers??

1 件のコメント

Star Strider
Star Strider 2017 年 7 月 12 日
Essential duplicate of: i have the following array

回答 (1 件)

Walter Roberson
Walter Roberson 2017 年 7 月 12 日

1 投票

Z(randperm(length(Z),10))

5 件のコメント

Jan
Jan 2017 年 7 月 12 日
編集済み: Jan 2017 年 7 月 12 日
Or if repetitions are wanted:
Z(randi([1, length(Z)], 1, 10))
mohammed elmenshawy
mohammed elmenshawy 2017 年 7 月 12 日
編集済み: mohammed elmenshawy 2017 年 7 月 12 日
No . I want to randomly select from the previous numbers and not from the length of the Z array? when apply this code, show the following
Adam
Adam 2017 年 7 月 12 日
You didn't use the code correctly. You don't assign it to Z, you index into Z.
Walter Roberson
Walter Roberson 2017 年 7 月 12 日
Breaking it up into steps:
random_index = randperm(length(Z));
Z(random_index(1:10))
That is, you start by taking a random permutation of the 31 possible indices into Z. Then you take 10 of those, so you get 10 unique indices into Z. Then you index Z at those locations to have chosen 10 random elements without replacement.
The code
Z(randperm(length(Z),10))
does this in one step.
Jan
Jan 2017 年 7 月 12 日
@mohammed: You do not need:
Z = (randperm(length(Z), 10));
but:
SelectedZ = Z(randperm(length(Z), 10));

この質問は閉じられています。

タグ

質問済み:

2017 年 7 月 12 日

閉鎖済み:

2021 年 8 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by