フィルターのクリア

Updating columns of a matrix

1 回表示 (過去 30 日間)
Atinesh Singh
Atinesh Singh 2017 年 4 月 22 日
コメント済み: Geoff Hayes 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by