How to use union operator

1 回表示 (過去 30 日間)
Atinesh Singh
Atinesh Singh 2017 年 3 月 27 日
回答済み: KSSV 2017 年 3 月 27 日
Consider the below code where I'm generating two random Population A and P. I need to make union of these two population. I tried using union operator, But it's not working.
clc; clear all;
varMin = -5;
varMax = 5;
D = 3; % Dimension
VarSize = [1 D]; % Decision Variables Matrix Size
NP1 = 10; % Population 1 size
NP2 = 3; % Population 2 size
empty_individual.Position = [];
empty_individual.Cost = [];
P = repmat(empty_individual, NP1, 1); % Population 1
A = repmat(empty_individual, NP2, 1); % Population 2
CostFunction = @(x) sphere(x);
%%Generating random population 1
for i = 1:NP1
P(i).Position = unifrnd(varMin, varMax, VarSize);
P(i).Cost = CostFunction(P(i).Position);
end
%%Generating random population 2
for i = 1:NP2
A(i).Position = unifrnd(varMin, varMax, VarSize);
A(i).Cost = CostFunction(A(i).Position);
end
sphere.m
function ret = sphere(x)
ret = sum(x.^2);
end
How to make union of A and P.

採用された回答

KSSV
KSSV 2017 年 3 月 27 日
% megre two structure
iwant = [P ; A] ;
% get unique values
[val,idx] = unique([iwant.Cost]) ;
iwant = iwant(idx) ;

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRandom Number Generation についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by