Borrar filtros
Borrar filtros

Updating columns of a matrix

1 visualización (últimos 30 días)
Atinesh Singh
Atinesh Singh el 22 de Abr. de 2017
Comentada: Geoff Hayes el 22 de Abr. de 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))';
empty_individual.Position = [];
subpop = repmat(empty_individual, nPop, 1);
% Intializing sub Population
for i = 1:nPop
subpop(i).Position = [1 1 1];
  1 comentario
Geoff Hayes
Geoff Hayes el 22 de Abr. de 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.

Iniciar sesión para comentar.

Respuestas (0)


Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by