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

回答(1 个)

Walter Roberson
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
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.
Prabha Kumaresan
Prabha Kumaresan 2017-12-26
编辑:Walter Roberson 2017-12-26
ok.
Could you tell me how to calculate rows minus (remaining groups minus 1) and use that as the upper bound of sz.
but when I run the following code I am getting the following errors:
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=7;%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 10]);% 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}];
E=[sumB{1}; sumB{2};sumB{3};sumB{4};sumB{5};sumB{6};sumB{7}];
end
end
e=randi([1 sz])
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.
>> idx=randsample(1:size(C_copy,1),e)
Error using randsample (line 131)
K must be less than or equal 0 for sampling
without replacement.
>> B{d} = C_copy(idx,:)
Index exceeds matrix dimensions.
>> C_copy(idx,:) = []
Index of element to remove exceeds matrix
dimensions.
>> sumB{d} = repmat(sum(B{d},1), [rows, 1])
Error using sum
Too many input arguments.

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by