Error in matlab codes

1 回表示 (過去 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 Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by