Filter löschen
Filter löschen

shuffle numbers in a vector

678 Ansichten (letzte 30 Tage)
Miriam
Miriam am 9 Nov. 2011
Kommentiert: PANKAJ VADHVANI am 4 Apr. 2023
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!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 9 Nov. 2011
YourVector(randperm(length(YourVector))
  5 Kommentare
Sakib Mahmud
Sakib Mahmud am 11 Jan. 2023
Bearbeitet: Sakib Mahmud am 11 Jan. 2023
YourVector = YourVector(randperm(length(YourVector)));
PANKAJ VADHVANI
PANKAJ VADHVANI am 4 Apr. 2023
Thanks a lot :)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Tomas
Tomas am 5 Feb. 2016
Bearbeitet: Tomas am 5 Feb. 2016
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 Kommentare
Peeyush Awasthi
Peeyush Awasthi am 9 Feb. 2019
It's indeed an smart solution to use randperm for any vector ar an array.
Chetna Patel
Chetna Patel am 29 Mär. 2021
Thanks for this solution.

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 5 Feb. 2016

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by