Randomly assign elements of one vector into different vectors?
13 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello I have the next vector:
v= [1 2 2 3 1 3 2 1 3 2 1 3 2 1 3 1 2 3 1 3]
And I need to assign randomly 4 elements of this vector in v1 other 3 elements in v2 other 5 elements in v3, 5 in v4 and 3 in v5 but without replacement, so each time these elements are assign to the first vector can not be assing to second and so on.
Is possible to do this with the randsample command?
2 Kommentare
Image Analyst
am 16 Aug. 2018
Bearbeitet: Image Analyst
am 16 Aug. 2018
When you say "in v1" do you really mean assign to v1? In other words, get the specified number of elements from v, and replace some elements in an existing v1 with the values from v? What values do v1, v2, ... v5 have before replacing the values, or do they not exist at all yet?
Akzeptierte Antwort
jonas
am 16 Aug. 2018
Bearbeitet: jonas
am 16 Aug. 2018
Just shuffle your vector and then split it
v=v(randperm(length(v)))
V = mat2cell(v,1,[4 3 5 5 3])
2 Kommentare
Stephen23
am 17 Aug. 2018
+1 simple and effective.
I would recommend using a different name for the output: having two variables that differ only by case is just asking for bugs!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Whos 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!