Hi, I have a vector which has n by 1 entries. I want to randomly shuffle the numbers in this vector! Thanks in advance for any sugestions regarding that!

 채택된 답변

Walter Roberson
Walter Roberson 2011년 11월 9일

25 개 추천

YourVector(randperm(length(YourVector))

댓글 수: 5

Miriam
Miriam 2011년 11월 9일
great! thanks a lot!!!
Alec Nagel
Alec Nagel 2018년 12월 3일
One ) missing at the end, but thanks! :)
SREENIVASULU REDDY KAMALAPURAM
SREENIVASULU REDDY KAMALAPURAM 2021년 11월 19일
Thanks a lot :))
Sakib Mahmud
Sakib Mahmud 2023년 1월 11일
편집: Sakib Mahmud 2023년 1월 11일
YourVector = YourVector(randperm(length(YourVector)));
PANKAJ VADHVANI
PANKAJ VADHVANI 2023년 4월 4일
Thanks a lot :)

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

추가 답변 (2개)

Tomas
Tomas 2016년 2월 5일
편집: Tomas 2016년 2월 5일

6 개 추천

To shuffle vectors without saving them to a variable first, e.g. to shuffle a for-loop, I recommend adding a function like this to your repertoire:
function v=shuffle(v)
v=v(randperm(length(v)));
end
e.g.
for i=shuffle(1:10)
disp(i)
end

댓글 수: 3

shuffle = @(v)v(randperm(numel(v)));
Peeyush Awasthi
Peeyush Awasthi 2019년 2월 9일
It's indeed an smart solution to use randperm for any vector ar an array.
Chetna Patel
Chetna Patel 2021년 3월 29일
Thanks for this solution.

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2011년 11월 9일

댓글:

2023년 4월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by