Updating columns of a matrix

2 views (last 30 days)
Atinesh Singh
Atinesh Singh on 22 Apr 2017
Commented: Geoff Hayes on 22 Apr 2017
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 Comment
Geoff Hayes
Geoff Hayes on 22 Apr 2017
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.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by