Unable to perform assignment because the left and right sides have a different number of elements.

27 views (last 30 days)
clear all
clc
syms s1 s2 m1 m2 j1 j2
lambda = 1060*10^-9;
wo = 0.02;
C = 10^(-7);
M=0;
b=0.1;
x=0
x = 0
y=0
y = 0
z=linspace(0,2000)
z = 1×100
0 20.2020 40.4040 60.6061 80.8081 101.0101 121.2121 141.4141 161.6162 181.8182 202.0202 222.2222 242.4242 262.6263 282.8283 303.0303 323.2323 343.4343 363.6364 383.8384 404.0404 424.2424 444.4444 464.6465 484.8485 505.0505 525.2525 545.4545 565.6566 585.8586
I = zeros(size(z));
k=2*pi/lambda;
po=(0.545*C^2*k^2*z).^(-(3/5));
delta= ((1i*k)./(2*z))+(1./(wo.^2))+(1./(po.^2));
c_delta= subs(delta, 1i, -1i);
etta = c_delta - (1./(delta.*po.^4));
alpha = (1i.*k./(2.*z)).*(1./(delta.*po.^2)-1);
beta_p = (b./(2.*wo)).*(1./(delta.*po.^2)+1);
beta_n = (b./(2.*wo)).*(1./(delta.*po.^2)-1);
A = (alpha.^2./etta)-((k.^2)-(4.*z.^2.*delta));
B1_p = ((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_p)./etta);
B1_n = ((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_n)./etta);
B2_p = -(((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_p)./etta));
B2_n = -(((1i.*k.*b)./(2.*z.*wo.*delta))+((2.*alpha.*beta_n)./etta));
C1_p = ((b.^2)./(4.*wo.^2.*delta))+(((beta_p).^2)./etta);
C1_n = ((b.^2)./(4.*wo.^2.*delta))+(((beta_n).^2)./etta);
C2_p = ((b.^2)./(4.*wo.^2.*delta))+(((beta_n).^2)./etta);
C2_n = ((b.^2)./(4.*wo.^2.*delta))+(((beta_p).^2)./etta);
for i = 1:100
for m1=0:M
for m2=0:M
for s1=0:m1/2
for j1=0:m1-2*s1
for s2=0:(M-m1)/2
for j2=0:M-m1-2*s2
G_1=sum((factorial(m1).*(-1).^s1)./(factorial(s1).*factorial(m1-2.*s1)).*(2.*1i./sqrt(delta)).^(m1-2*s1),'all');
G_2=sum(factorial(m1-2.*s1)./(factorial(j1).*factorial(m1-2.*s1-j1)).*((1./(2.*1i.*sqrt(etta))).^(m2+j1)).*(1./po.^2).^j1,'all');
G_p3=((1i.*k.*x)./(2.*z(i))+(b./2.*wo)).^(m1-2.*s1-j1).*exp((A.*x.^2)+(B1_p.*x)+C1_p).*hermiteH(m2+j2,((1i.*alpha.*x)./sqrt(etta))+((1i.*beta_p)./sqrt(etta)));
G_p4=((1i.*k.*x)./(2.*z(i))-(b./2.*wo)).^(m1-2.*s1-j1).*exp((A.*x.^2)+(B2_n.*x)+C2_n).*hermiteH(m2+j2,((1i.*alpha.*x)./sqrt(etta))-((1i.*beta_n)./sqrt(etta)));
G_p=G_1.*G_2.*(G_p3 + G_p4);
G_n3=((1i.*k.*x)./(2.*z(i))+(b./2.*wo)).^(m1-2.*s1-j1).*exp((A.*x.^2)+(B1_n.*x)+C1_n).*hermiteH(m2+j2,((1i.*alpha.*x)./sqrt(etta))+((1i.*beta_n)./sqrt(etta)));
G_n4=((1i.*k.*x)./(2.*z(i))-(b./2.*wo)).^(m1-2.*s1-j1).*exp((A.*x.^2)+(B2_p.*x)+C2_p).*hermiteH(m2+j2,((1i.*alpha.*x)./sqrt(etta))-((1i.*beta_p)./sqrt(etta)));
G_n=G_1.*G_2.*(G_n3 + G_n4);
H_1=sum((factorial(M-m1).*(-1).^s2)./(factorial(s2).*factorial(M-m1-2.*s2)),'all').*(2.*1i./sqrt(delta)).^(M-m1-2.*s2);
H_2=sum(factorial(M-m1-2.*s2)./(factorial(j2).*factorial(M-m1-2.*s2-j2)),'all')*((1./(2.*1i.*sqrt(etta))).^(M-m2+j2)).*(1./po.^2).^j2;
H_p3=((1i.*k.*y)./(2.*z(i))+(b./2.*wo)).^(M-m1-2.*s2-j2).*exp((A.*y.^2)+(B1_p.*y)+C1_p).*hermiteH(M-m2+j2,((1i.*alpha.*y)./sqrt(etta))+((1i.*beta_p)./sqrt(etta)));
H_p4=((1i.*k.*y)./(2.*z(i))-(b./2.*wo)).^(M-m1-2.*s2-j2).*exp((A.*y.^2)+(B2_n.*y)+C2_n).*hermiteH(M-m2+j2,((1i.*alpha.*y)./sqrt(etta))-((1i.*beta_n)./sqrt(etta)));
H_p=H_1.*H_2.*(H_p3 + H_p4);
H_n3=((1i.*k.*y)./(2.*z(i))+(b./2.*wo)).^(M-m1-2.*s2-j2).*exp((A.*y.^2)+(B1_n.*y)+C1_n).*hermiteH(M-m2+j2,((1i.*alpha.*y)./sqrt(etta))+((1i.*beta_n)./sqrt(etta)));
H_n4=((1i.*k.*y)./(2.*z(i))-(b./2.*wo)).^(M-m1-2.*s2-j2).*exp((A.*y.^2)+(B2_p.*y)+C2_p).*hermiteH(M-m2+j2,((1i.*alpha.*y)./sqrt(etta))-((1i.*beta_p)./sqrt(etta)));
H_n=H_1.*H_2.*(H_n3 + H_n4);
G=G_p+G_n;
H=H_p+H_n;
I(i) =G.*H;
end
end
end
end
end
end
endUnable to perform assignment because the left and right sides have a different number of elements.
Unable to perform assignment because the left and right sides have a different number of elements.
plot(z,I)
xlabel('z')
ylabel('I')
xlim([0 8000])
ylim([0 1])

Accepted Answer

KSSV
KSSV on 28 Jun 2022
Error is simple.... you are trying to save more number of elements in an array then it is initialized for.
Example:
A = zeros(3,3) ; % 3x3 matrix initialized
A(1,:) = rand(1,3) ; % no error, in first row three elements saved
A(2,:) = 0 ; % no error, in second row all zeros saved
A(3,:) = rand(1,4); % error, you canntt save four elemnts in a place of three elements.
Unable to perform assignment because the size of the left side is 1-by-3 and the size of the right side is 1-by-4.
Similiarly, this line:
I(i) =G.*H;
RHS is 1x100 array; you are trying to save 100 elements in a single elemet. Replace that line with:
I(i,:) =G.*H;

More Answers (1)

Garmit Pant
Garmit Pant on 28 Jun 2022
Hello Athira
It is my understanding that you need help in resolving the error message :
% Unable to perform assignment because the left and right sides have a different number of elements.
Having gone through the code and running it myself, I can see the problem is arrising due to the following code snippet:
G=G_p+G_n;
H=H_p+H_n;
I(i) =G.*H;
G and H are row vectors with size 1x100. Since you are using the element wise multiplication operation .* the result of G.*H will also be a row vector of the size 1x100.
I has been defined as:
I = zeros(size(z))
Thus I has size 1x100, with each element of I being a zero. Thus, I(i) has a size of 1x1. It is due to this mismatch of sizes that the assignment cannot be performed.
You can refer to this article to learn more about Array and Matrix operations: https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by