Error in matlab codes
5 views (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Image Analyst
on 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));
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!