필터 지우기
필터 지우기

Error in matlab codes

조회 수: 2 (최근 30일)
Usman Taya
Usman Taya 2019년 6월 17일
편집: Usman Taya 2019년 6월 17일
Hi everyone,
I write following codes:
clear all
close all
clc
% Define the details of the problem
nVar = 4;
ub = [10 10 10 10];
lb = [-10 -10 -10 -10];
fobj = @ObjectiveFunction;
% Define the SFA's paramters
noP = 20;
maxIter = 50;
% The SFA algorithm
% Initialize the Fish position and food
for k = 1 : noP
Swarm.Fish(k).X = (ub-lb) .* rand(1,nVar) + lb;
FishFitness = Swarm.Fish(k).X;
Swarm.Fish(k).Fitness = fobj(FishFitness);
Swarm.Food(k).X = zeros(1,nVar);
Swarm.Food(k).Fitness = inf;
end
[sorted_fish_fitness,sorted_indexes]=sort(FishFitness);
for newindex=1:noP
Sorted_fish(newindex)=[Swarm.Fish.X(sorted_indexes(newindex))];
end
Swarm.Food.X=Sorted_fish;
Swarm.Food.Fitness=sorted_fish_fitness;
When I run it it shows following error
Expected one output from a curly brace or dot indexing expression, but there were 20 results.
Error in SFA (line 28)
Sorted_fish(newindex)=[Swarm.Fish.X(sorted_indexes(newindex))];
kindly help me to solve this problem.

답변 (1개)

Image Analyst
Image Analyst 2019년 6월 17일
Try getting rid of the brackets, which concatenate all the values from all the structures into a single array of 20 values, which can't go into a single index of Sorted_fish.
Sorted_fish(newindex) = Swarm.Fish.X(sorted_indexes(newindex));
  댓글 수: 1
Usman Taya
Usman Taya 2019년 6월 17일
편집: Usman Taya 2019년 6월 17일
Thank you for answer but after removing brackets it shows following error
Expected one output from a curly brace or dot indexing expression, but there were 20 results.
Error in SFA (line 28)
Sorted_fish(newindex)=Swarm.Fish.X(sorted_indexes(newindex));

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by