Borrar filtros
Borrar filtros

Error in matlab codes

2 visualizaciones (últimos 30 días)
Usman Taya
Usman Taya el 17 de Jun. de 2019
Editada: Usman Taya el 17 de Jun. de 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.

Respuestas (1)

Image Analyst
Image Analyst el 17 de Jun. de 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 comentario
Usman Taya
Usman Taya el 17 de Jun. de 2019
Editada: Usman Taya el 17 de Jun. de 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));

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by