Could anyone tell me how to execute the following code
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
ANKUR KUMAR
am 20 Dez. 2017
Bearbeitet: ANKUR KUMAR
am 20 Dez. 2017
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 Kommentar
Prabha Kumaresan
am 21 Dez. 2017
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
am 20 Dez. 2017
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 Kommentare
Rik
am 21 Dez. 2017
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
am 21 Dez. 2017
Could you tell me how to get the user values by indexing it.
Siehe auch
Kategorien
Mehr zu Logical 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!