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

조회 수: 1 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2020년 10월 22일
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.

추가 답변 (1개)

KSSV
KSSV 2020년 10월 22일
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) ;

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by