필터 지우기
필터 지우기

A question about randomsample syntax

조회 수: 1 (최근 30일)
Jianshe Feng
Jianshe Feng 2016년 12월 14일
편집: KSSV 2016년 12월 14일
I noticed that there are some syntax about how to "randomsample", for example"
y = randsample(n,k)
y = randsample(population,k)
Here is my concern: what if my "population" only has ONE element? for example, suppose my population is [3]. Then what I want by using randomsample(3,1) is to get 3, however, MATLAB will give me a number from [1,2,3] randomly. The reason is obvious - MATLAB takes the 3 as "n" but not "population". Does any one know how to solve this problem?
My application is to use randomsample to write an equipartition function. If you're interested in, my code is as follow:
function group = equipartitionGroup(sample,groupNo)
sampLen = length(sample);
for i = 1:mod(sampLen,groupNo)
k = ceil(sampLen/groupNo);
group{i} = randsample(sample,k);
sample = setdiff(sample,group{i});
end
for i = mod(sampLen,groupNo)+1:groupNo
k = floor(sampLen/groupNo);
group{i} = randsample(sample,k);
sample = setdiff(sample,group{i});
end
I appreciate it if any expert can help to fix or improve it. Thanks.

답변 (1개)

KSSV
KSSV 2016년 12월 14일
편집: KSSV 2016년 12월 14일
Please check that randsample(3,1) always returns only one output. If you want only one output which is fixed 3. You may use like below:
k = repmat(3,1,3) ;
randsample(k,1)
If you enter randsample(3,3) then the output will be 1 to 3.

카테고리

Help CenterFile Exchange에서 Correlation and Convolution에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by