How to use union operator

조회 수: 12 (최근 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에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by