MATLAB: Could anyone clarify the doubt with respect to the following code

idx

N_UE=10
unused_rows=1:N_UE
while ~isempty(unused_rows)
N_UE_rows=ceil(sqrt(randi(numel(unused_rows))))
rows=unused_rows(randsample(length(unused_rows),N_UE_rows))
[~,idx]=find(ismember(unused_rows,rows))
unused_rows(idx)=[]
end
If i run the code it executes My clarification is with respect to command line [~,idx]=find(ismember(unused_rows,rows)) after executing the code the result in the command window was
N_UE =
7
unused_rows =
1 2 3 4 5 6 7
N_UE_rows =
2
rows =
6 4
idx =
4 6
unused_rows =
1 2 3 5 7
N_UE_rows =
2
rows =
1 3
idx =
1 3
unused_rows =
2 5 7
N_UE_rows =
2
rows =
5 7
idx =
2 3
unused_rows =
2
N_UE_rows =
1
rows =
2
idx =
1
unused_rows =
Empty matrix: 1-by-0
If rows= 6 4
idx=4 6
rows = 1 3
idx=1 3
but if
rows= 5 7
idx should be 5 7 but it shows
idx = 2 3
rows = 2
idx=1
why it is so.Could anyone clarify my doubt

Best Answer

Use the following code:
N_UE=10;
unused_rows_init=1:N_UE;
unused_rows=unused_rows_init;
while ~isempty(unused_rows)
N_UE_rows=ceil(sqrt(randi(numel(unused_rows))))
rows=unused_rows(randsample(length(unused_rows),N_UE_rows))
[~,idx]=find(ismember(unused_rows_init,rows))
unused_rows(ismember(unused_rows,rows))=[]
end