In the below code, We have a random population with structures Position (3D array) and Cost. I need to check whether a particular element present in the population If it does then I want to remove it from the population. I'm using 'find' function for doing it. It was working fine with arrays but with structures I'm getting error below error
_ Undefined operator '~=' for input arguments of type 'struct'_
Here is the code
clc; clear all;
varMin = -5;
varMax = 5;
D = 3;
VarSize = [1 D]; % Decision Variables Matrix Size
NP = 3;
empty_individual.Position = [];
empty_individual.Cost = [];
P = repmat(empty_individual, NP, 1);
addpath('E:\Dimensionality Reduction\DR & Optimization\Data\Objective Functions');
CostFunction = @(x) sphere(x);
%%Generating random population
for i = 1:NP
P(i).Position = unifrnd(varMin, varMax, VarSize);
P(i).Cost = CostFunction(P(i).Position);
end
xi = [1 1 1]; % xig
P = P(find(P ~= xi));

댓글 수: 4

KSSV
KSSV 2017년 3월 28일
You cannot use find like that. _P is 3x1 structure, what you want to find exactly?
Atinesh Singh
Atinesh Singh 2017년 3월 28일
Can you suggest any alternative for it.
KSSV
KSSV 2017년 3월 28일
You have to mention what you are expecting actually?
Atinesh Singh
Atinesh Singh 2017년 3월 28일
I need to check whether a particular element present in the population If it does then I want to remove it from the population

댓글을 달려면 로그인하십시오.

 채택된 답변

KSSV
KSSV 2017년 3월 28일

0 개 추천

idx = 1:length(P) ;
P = P(find(idx ~= xi));

댓글 수: 6

Atinesh Singh
Atinesh Singh 2017년 3월 28일
Its not working "I need to check whether a particular element present in the population If it does then I want to remove it from the population"
KSSV
KSSV 2017년 3월 28일
Your xi is [1 1 1]..P has indices [1 2 3]. [1] is common with xi and P, so the above code picks up only [2 3] indices of P.
Atinesh Singh
Atinesh Singh 2017년 3월 28일
Population has two row Position and cost. Lets say I have a population initialized as (Position; Cost) (1 1 1; 3) (2 2 2; 12) (3 3 3; 27)
Say xi = (2 2 2) #Position
As it is in the population then member corresponding to it should be removed from the population. New population should be
(1 1 1; 3) (3 3 3; 27)
Pos = reshape([P.Position] ,3,[])' ;
[val,idx] = ismember(xi,Pos,'rows') ;
pick = setdiff(1:length(P),idx) ;
iwant = P(pick) ;
Atinesh Singh
Atinesh Singh 2017년 3월 28일
is there any other way to do it
KSSV
KSSV 2017년 3월 28일
Define xi as a structure..And try using ismemeber etc.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2017년 3월 28일

댓글:

2017년 3월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by