How to fix : Unable to perform assignment because the left and right sides have a different number of elements.
2 次查看(过去 30 天)
显示 更早的评论
I am trying to use particle swarm optimization to solve a problem of mine.
And i get following error:
Unable to perform assignment because the left and right sides have a
different number of elements.
This is the initial code:
function output= fun(X)
g(:,1)=[2;2]; R1=X(:,1); R3=X(:,2); R2= R3-g; n=X(:,3);
Br1=1.2;Br2=1.2;mu=4.*pi.*10.^-7;R4=35;
Length=70; L=2.*pi.*sqrt((R4.^2+R1.^2)./2);
x=(Br1.*Br2.*L.*(10^-6))/(4.*pi.*mu);
a=Length./n;
b=R2-R1;
d=R4-R3;
h=R3-R2;
c=-10:0.5:0;
F=0;
I feel there is an issue with how i have defined the variables.
2 个评论
Dyuman Joshi
2023-1-18
What is the full error and in which line does it occur?
Mention the code that is relevant to the line where the error occurs.
回答(1 个)
KSSV
2023-1-18
This is a simple error, it occurs when you try to save more number of elements into an array than it is initialized for.
Ex:
A = zeros(1,3) ;
A(1) = rand ; % filling one element in one position
A(2) = rand(1,2) ; % filling two elements in one position; not correct so error
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Particle Swarm 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!