Hello. I have an array: A=[4 8 7 9 5] How to I randomly select an element from the array? Thanks a lot.

 채택된 답변

KL
KL 2017년 11월 16일
편집: KL 2017년 11월 16일

1 개 추천

댓글 수: 3

Vero Alin
Vero Alin 2017년 11월 16일
Thank you. I have just tried that but when i am running the code several times, I am getting only 9. Is it really random?
KL
KL 2017년 11월 16일
편집: KL 2017년 11월 16일
Yes, it is. The documentation says so. You could test it like,
A=[4 8 7 9 5];
A_rand = zeros(1,15);
for k = 1:numel(A_rand)
A_rand(1,k) = randsample(A,1);
end
now the result is,
A_rand =
5 5 5 4 9 8 7 5 9 8 4 7 5 7 4
Another alternative is to create the index randomly but that's pretty much the same,
A(randi([1 numel(A)],1)) or A(randperm(numel(A),1))
Vero Alin
Vero Alin 2017년 11월 16일
Alright. Thank you. Much appreciated.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

2017년 11월 16일

댓글:

2017년 11월 16일

Community Treasure Hunt

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

Start Hunting!

Translated by