Greetings,
I have a vector of 12 elements (a = [10 20 35 38 40 45 48 50 55 58 60 75]') and I want to randomly select one element at a time. How can I do that? Thank you very much.

댓글 수: 1

Stephen23
Stephen23 2017년 1월 4일
편집: Stephen23 2017년 1월 4일
Adam's answer is the best use of MATLAB because it returns a vector, and it does not require the Statistics Toolbox.

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

 채택된 답변

Adam
Adam 2017년 1월 4일
편집: Adam 2017년 1월 4일

1 개 추천

randA = a( randperm( numel(a) ) )
will give you them in a random order all at once. Then you can take them in turn if you wish.

댓글 수: 2

how can i do that? thank you so much.
b = a(randperm(numel(a)));
for k = 1:numel(b)
b(k)
end

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

추가 답변 (1개)

KSSV
KSSV 2017년 1월 4일
편집: KSSV 2017년 1월 4일

1 개 추천

a = [10 20 35 38 40 45 48 50 55 58 60 75] ;
N = length(a) ;
pos = 1:N ;
idx = randsample(pos,N) ;
for i = 1:N
a(idx(i))
end

댓글 수: 2

Thank you so much. I see in my output when running your script that all elements are accessed, how can I modify your script to get only one element at a time as an ouput and not all of them? (sorry, totally new to matlab)
randsample(a,1)

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

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

태그

질문:

2017년 1월 4일

댓글:

2017년 1월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by