How to create a random array from an array

조회 수: 10 (최근 30일)
arash rad
arash rad 2023년 2월 16일
댓글: Steven Lord 2023년 2월 16일
Hello every one
I have a 1x450 array and I want to choose 300 elements of this array randomly and create a new array with random elements
I tried randi and randperm but these two didn't give me the answer I want
What should I use instead
Thanks in advance
  댓글 수: 2
Dyuman Joshi
Dyuman Joshi 2023년 2월 16일
What did you try?
Do you want 300 different indices? or 300 indices with repetitions works?
Jan
Jan 2023년 2월 16일
randi creates indices with repetitions, randperm without. If both methods are not working for you, the problem is hidden inside "didn't give me the answer I want". The readers cannot guess, what you have exactly tried and what you want. So please post your code and explain, what you want to change.

댓글을 달려면 로그인하십시오.

채택된 답변

Arif Hoq
Arif Hoq 2023년 2월 16일
try this:
A=randi(500,1,450);
B=A(randperm(numel(A),300));% choose 300 elements randomly
  댓글 수: 2
arash rad
arash rad 2023년 2월 16일
I try this but sth I don't want is that from the 1x450 array no repeated element the new array
for example array index(20) in matrix A only comes one time in B but it sometimes happen more than one
Steven Lord
Steven Lord 2023년 2월 16일
As Jan said, randi draws with replacement and so may generate the same number more than once. randperm draws without replacement and so won't.
rng default % for reproducibility
x = randi(10, 1, 5)
x = 1×5
9 10 2 10 7
y = randperm(10, 5)
y = 1×5
7 3 1 9 10
The number 10 does appear in x twice. If we use the analogy of a deck of cards, in x I selected five cards, replacing each one back in the deck after I've noted what it was. In y I dealt out a hand of five cards without putting any back on the deck.
Note that using y to index into another array may produce the same number repeatedly in that result if the array you're indexing into contains that number more than once. In the deck analogy:
deck = repmat(1:13, 4, 1)
deck = 4×13
1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13 1 2 3 4 5 6 7 8 9 10 11 12 13
suits = repmat(["club"; "diamond"; "heart"; "spade"], 1, 13)
suits = 4×13 string array
"club" "club" "club" "club" "club" "club" "club" "club" "club" "club" "club" "club" "club" "diamond" "diamond" "diamond" "diamond" "diamond" "diamond" "diamond" "diamond" "diamond" "diamond" "diamond" "diamond" "diamond" "heart" "heart" "heart" "heart" "heart" "heart" "heart" "heart" "heart" "heart" "heart" "heart" "heart" "spade" "spade" "spade" "spade" "spade" "spade" "spade" "spade" "spade" "spade" "spade" "spade" "spade"
cards = deck(y)
cards = 1×5
2 1 1 3 3
cardsuits = suits(y)
cardsuits = 1×5 string array
"heart" "heart" "club" "club" "diamond"
The first 1 in y corresponds to the Ace of hearts and the second corresponds to the Ace of clubs. If you only wanted to draw at most one of each rank of card (at most one Ace or at most one Queen, for example, regardless of suit) then that would require a little modification.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by