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 CenterFile Exchange 中查找有关 Random Number Generation 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by