Could any one tell me how to overcome the Error using randi First input must be a positive scalar integer value IMAX, or two integer values [IMIN IMAX] with IMIN less than or equal to IMAX. Error in test (line 22) e=randi([1
7 次查看(过去 30 天)
显示 更早的评论
clear all;
clc;
N_UE=[10 20 30 40 50];
N_SC=[60 70 80 90 100];
for t= 1:length(N_UE)
for r = 1:length(N_SC)
C=rand(N_UE(t),N_SC(r))
N_G=10;%No of group
B = cell(N_G,1);
sumB = cell(N_G,1);
C_copy = C;
for d=1:1:N_G
if d==N_G
sz=length(C_copy(:,1));
e=sz;
else
if d==1
e=randi([2 5]);% select No of UE randomly
else
sz=length(C_copy(:,1));
sz=ceil(sz/2);
e=randi([1 sz]);% select No of UE randomly
end
end
idx=randsample(1:size(C_copy,1),e);
B{d} = C_copy(idx,:);
C_copy(idx,:) = [];
[rows, columns] = size(B{d});
% Get sum of columns and replicate vertically.
sumB{d} = repmat(sum(B{d},1), [rows, 1]);
end
E=[sumB{1}; sumB{2};sumB{3};sumB{4};sumB{5};sumB{6};sumB{7};sumB{8};sumB{9};sumB{10}];
end
end
If i run this code i am getting Error using randi
First input must be a positive scalar
integer value IMAX, or two integer
values [IMIN IMAX] with IMIN less
than or equal to IMAX.
Error in test (line 22)
e=randi([1 sz]);% select No of UE randomly
Please help me to overcome it
0 个评论
回答(1 个)
Walter Roberson
2017-12-26
编辑:Walter Roberson
2017-12-26
Your C_copy is becoming empty, so length() of it is becoming 0, and then you are asking for randi([1 0])
Have you considered the possibility of using how to use the debugger?
5 个评论
Walter Roberson
2017-12-26
You can calculate rows minus (remaining groups minus 1) and use that as the upper bound of sz. That would cause sizes to be distributed randomly until the number of remaining rows equaled the number of groups at which point it would allocate one row each to the remaining groups. This would not work out as a "fair" distribution as the groups allocated first would tend to be larger, but at least it would not exceed matrix bounds.
If I recall correctly I already posted a link for you to the discussion in which Roger and I were discussing integer partitioning with and without constraints. I presented code there that was only a few lines. Roger was concerned that my code tended to pick groups more equal in size than would be expected if you enumerated all of the possibilities and picked one at random, but I suspect that my code there would be fine for your purposes.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!