Updating columns of a matrix
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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 Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!