MATLAB: Could anyone help me to solve the error in the following code

clustering

N_UE=6
N_SC=16
iwant = cell(length(N_UE),1)
for t= 1:length(N_UE)
for r= 1:length(N_SC)
G=rand(N_UE(t),N_SC(r))
N_SC_=ceil(N_SC(r)/N_UE(t))%round up to nearest multiple
C=repmat(diag(1:N_UE(t)),1,N_SC_ )
iwant{t}=C(:,1:N_SC(r))%crop back to only needed cols
D = iwant{t}
unused_rows=1:N_UE
E_part=cell(1)
while ~isempty(unused_rows)
N_UE_rows=ceil(sqrt(randi([2,numel(unused_rows)])))
rows=unused_rows(randsample(length(unused_rows),N_UE_rows))
[~,idx]=find(ismember(unused_rows,rows))
unused_rows(idx)=[]
[E,E_part]=cluster_rows(G,D,rows) % calling function
end
end
end
If i run the 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 three (line 23) N_UE_rows=ceil(sqrt(randi([2,numel(unused_rows)])))
Could anyone help me to solve it.

Best Answer

You didn't use a check to see if you were leaving only 1 value for the last iteration.
N_UE_rows=ceil(sqrt(randi([2, numel(unused_rows)])));
if (N_UE_rows+1)==numel(unused_rows)
N_UE_rows=numel(unused_rows);
end