Error in matlab codes

3 Ansichten (letzte 30 Tage)
Usman Taya
Usman Taya am 17 Jun. 2019
Bearbeitet: Usman Taya am 17 Jun. 2019
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.

Antworten (1)

Image Analyst
Image Analyst am 17 Jun. 2019
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 Kommentar
Usman Taya
Usman Taya am 17 Jun. 2019
Bearbeitet: Usman Taya am 17 Jun. 2019
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));

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by