Randomly assign elements of one vector into different vectors?

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

Image Analyst
Image Analyst 2018년 8월 16일
편집: Image Analyst 2018년 8월 16일
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?
No sorry, I mean in vector 1 so four elements of the v vector assign them in vector 1

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

 채택된 답변

jonas
jonas 2018년 8월 16일
편집: jonas 2018년 8월 16일
Just shuffle your vector and then split it
v=v(randperm(length(v)))
V = mat2cell(v,1,[4 3 5 5 3])

댓글 수: 2

+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!
This worked perfectly thanks!

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

추가 답변 (0개)

카테고리

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

질문:

2018년 8월 16일

댓글:

2018년 8월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by