how to remove the index number

1 次查看(过去 30 天)
Could anybody help me how to remove the index number in for loop for the code mentioned below under the scenario it should not get repeated again in other group and for each run the matrix A should be updated.
A = diag([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15]);
N_G=4;%No of group
B = cell(N_G,1);
sumB = cell(N_G,1);
%
for i=1:1:N_G
n=randi([1 15],1)% select No of UE randomly
idx=randsample(1:size(A,2),n)
B{i} = A(idx,:)
%
[rows, columns] = size(B{i})
% Get sum of columns and replicate vertically.
sumB{i} = repmat(sum(B{i}, 1), [rows, 1])
end
  2 个评论
Stephen23
Stephen23 2017-12-21
"Could anybody help me how to remove the index number in for loop for the code mentioned below "
What index you want to remove? From where, precisely?
"under the scenario it should not get repeated again in other group and for each run the matrix A should be updated."
What does this mean? Please explain in detail.
Prabha Kumaresan
Prabha Kumaresan 2017-12-22
编辑:Stephen23 2017-12-22
Among the 15 rows if randomly any three rows form 1 group those rows should not be in any other group or repeated again.So their index needs to be removed inorder to avoid repeatation.As the index of the rows are removed they needs to be updated.The next for loop process starts from the updated one.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2017-12-21
It's not clear what you mean. A for loop has to have an index (loop iteration variable), though you're free to use it or ignore it in the loop. Also if you want A to change (somehow) in the loop, you're going to have to have A on the left side of an equals sign.
  3 个评论
Image Analyst
Image Analyst 2017-12-22
It looks like code I've given you at some point but that you've tried to modify to do something else. But I don't know what you're trying to do. For example, what is this line supposed to do:
for t= numGroups:length(N_UE)
This is essentially
for t= numGroups:numGroups
which means it will execute only once. I think you just need to think it through some more.
Prabha Kumaresan
Prabha Kumaresan 2017-12-22
编辑:Stephen23 2017-12-22
Yes.It was the code given by you .In your code at the beginning it was numUsers = 50,but i am trying to run the code with respect to an array which means
numUsers=diag([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20]);
If the code is in this form
diag([1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20])
N_UE = 1 : numUsers
% Get a scrambled array
s = N_UE(randperm(length(N_UE)))
numGroups = 5; % Need to divide s up into 5 groups
divisions = sort(randperm(numUsers-2, numGroups-1) + 1, 'ascend')
divisions = [0, divisions, numUsers]
% 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} = s(indexes);
end
celldisp(groups); % Display groups in command window.
If i run the code i am getting Error using randperm
Inputs must be nonnegative scalar
integers.
Error in two (line 6)
divisions = sort(randperm(numUsers-2,
numGroups-1) + 1, 'ascend').
could you please how to overcome it.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by