How to randomly extract 10 elements from that matrix

i generate 1000*1 matrix through 'A=rand(100,1)'. And now i want to randomly extract 10 elements from that matrix.
How can i get that?!

回答 (1 件)

Dyuman Joshi
Dyuman Joshi 2023 年 5 月 12 日

0 投票

A = rand(1000,1);
%10 random indices less than or equal to the number of elements in A
idx = randperm(numel(A),10)
idx = 1×10
178 548 432 843 471 260 520 944 697 282
out = A(idx)
out = 10×1
0.3810 0.0216 0.9089 0.3924 0.5069 0.7092 0.2685 0.4270 0.0852 0.3132

2 件のコメント

Yoon Ju
Yoon Ju 2023 年 5 月 13 日
Oh i got it-! thank you so much But i couldn't get how 'out' return that elements. Is 'out' a function?
Dyuman Joshi
Dyuman Joshi 2023 年 5 月 14 日
"out" is just the variable to which I have assigned the output. You can change the name of the output as per what you want.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeRandom Number Generation についてさらに検索

質問済み:

2023 年 5 月 12 日

コメント済み:

2023 年 5 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by