MATLAB Answers

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

4 views (last 30 days)
Ivanov Andrey
Ivanov Andrey on 22 Oct 2020
Answered: Walter Roberson on 22 Oct 2020
Hi! my code has the following error..
Unable to perform assignment because the left and right sides have a different number of elements.
Error in project_matlab/intens1 (line 270) s(j) = NeL.*ra1w.*10e-3;
Error in project_matlab (line 284) ILT = intens1(ra1w);
%^^blah blah blah
function roa = ra(E,EA,FH)
roa = exp(-((E-EA).^2)./(2*sigmaa^2)).*(1-fermi(E,FH));
end
%vv blahblahblah
Up to this point everything is ok.
ra1w = ra(Ef,EaC+kwk,Fh+kwk);
ra2w = ra(Ef,EaT+kwk,Fh+kwk);
function I1 = intens1(ra1w)
N = size(ra1w);
s = zeros(N);
for j = 1:N
s(j) = NeL.*ra1w.*10e-3;
end
IK1 = sum(s);
end
function I1 = intens2(ra2w)
N = size(ra2w);
s = zeros(N);
for j = 1:N
s(j) = NeG.*ra2w.*10e-3;
end
IK1 = sum(s);
end
ILT = intens1(ra1w);
IGT = intens2(ra2w);
ILC = intens1(ra1w);
IGC = intens2(ra2w);
How do I fix this? please help me.

  0 Comments

Sign in to comment.

Answers (2)

KSSV
KSSV on 22 Oct 2020
Please note that the error is simple...this occurs when you try to save more number of elements than intialized into an array.
Example:
% Initialization
A = zeros(3,2) ; % three rows and two columns
A(1,:) = rand(1,3) ; % intialized to have two columns, but you are saving three columns, this throws error
So, in your case check the dimensions and do intialization properly. If the dimensions are not known. USe cells
Exmaple:
A = cell(3,1) ;
A{1} = rand(1,3) ;
A{2} = rand ;
A{3} = rand(1,4) ;

  0 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 22 Oct 2020
N = size(ra1w);
s = zeros(N);
for j = 1:N
s(j) = NeL.*ra1w.*10e-3;
end
You clearly expect ra1w to be non-scalar but inside the loop you use all of it, doing exactly the same thing every iteration.
You are also using size incorrectly. size always returns at least two elements when called with one parameter, and you are using the entire vector as the second operand of the colon operator. Your code will fail unless the input happens to be a column vector.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!