Could anyone tell me how to execute the following code
조회 수: 3 (최근 30일)
이전 댓글 표시
N_UE=[2 4 6 8 10];
for t= 1:length(N_UE)
numGroups = 5;
divisions = sort(randperm(t, numGroups) , 'ascend')
divisions = [0, divisions, t]
% Create cell array with random number of users in each groups
groups = cell(1, numGroups);
for k = 1 : numGroups
indexes = divisions(k)+1:divisions(k+1);
% Assign users to the kth group:
usersInThisGroups = length(indexes);
fprintf('Assigning %d users (indexes %d to %d) to group %d.\n', ...
usersInThisGroups, divisions(k)+1,divisions(k+1), k);
groups{k} = t(indexes);
end
celldisp(groups);
end
If i run the above code i am getting Error using randperm K must be less than or equal to N.
Error in check (line 10) divisions = sort(randperm(t, numGroups) , 'ascend')
Please help me to fix this issue.
댓글 수: 0
답변 (2개)
ANKUR KUMAR
2017년 12월 20일
편집: ANKUR KUMAR
2017년 12월 20일
You problem of Error using randperm K must be less than or equal to N can be resolved by changing
divisions = sort(randperm(t, numGroups) , 'ascend')
to
divisions = sort(randperm( numGroups,t) , 'ascend')
After omitting this error, one more error is there in this line.
groups{k} = t(indexes);
In your program, t is the loop call variable, and at a time, t stores only one value. At the same time, indexes is of dimension 1*5. How can be expect from matlab to extract 1*5 location (which are there is indexes) from one number variable( t).
Could you please explain your motive behind using this line, so that we can help you completely to resolve your problem.
댓글 수: 1
Prabha Kumaresan
2017년 12월 21일
The intention of this code is to group users in a random manner and display the index of the users present in each group.
Rik
2017년 12월 20일
Read the error message and the documentation. What is supposed to happen when you have only 2 values and you ask Matlab to pick 5? The following line will let you run the code, but might not be what you mean.
divisions = sort(randperm(t,min([t numGroups])) , 'ascend')
Alternatively, you can start the loop at numGroups.
for t= numGroups:length(N_UE)
댓글 수: 5
Rik
2017년 12월 21일
t is only a scalar. It does not contain a list of users, so you can get the user values by indexing it.
Prabha Kumaresan
2017년 12월 21일
Could you tell me how to get the user values by indexing it.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!