Updating columns of a matrix

2 次查看(过去 30 天)
Atinesh Singh
Atinesh Singh 2017-4-22
Consider the below code where we have population 'pop' having 20 individuals with a dimension of 5 and a subpopulation 'subpop' having 20 individuals with dimension 0f 3. I need to update first 3 columns of 'pop' with columns of 'subpop' for all rows. I know it can be done easily using a for loop, But I was wondering if it is possible to do it in a single line of code.
nPop = 20; % Population Size (Swarm Size)
varMin = -5; % Lower Bound of Variables
varMax = 5; % Upper Bound of Variables
%%Cooperative Co-evolution
D = 5; % Dimension
empty_individual.Position = [];
pop = repmat(empty_individual, nPop, 1);
% Intializing Population
for i = 1:nPop
pop(i).Position = (varMin + (varMax + varMax)*rand(D, 1))';
end
empty_individual.Position = [];
subpop = repmat(empty_individual, nPop, 1);
% Intializing sub Population
for i = 1:nPop
subpop(i).Position = [1 1 1];
end
  1 个评论
Geoff Hayes
Geoff Hayes 2017-4-22
Atones - given that pop is a struct, I don't think that you can replace the first three columns of your Position member in a single line of code. Using a for loop, in my opinion, makes it more clear what you are trying to do.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by