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

1 次查看(过去 30 天)
I keep getting the error message above when I try to run this loop, I need to count the number of leap years there are and add up the total number of them equal to L, but when I run this loop, I only ever get L=1 from this for the answer, or it will give me the error above. It says that this error is from line 7: L(i)=L+0;
When I add (i) to the second L on the other side of the = it then tells me that the Index exceeds the number of array elements (1).
x=[1:1:2021];
L(1)=0
for i = 1:2021
if mod(x(i),400) == 0
L(i)=L+1;
elseif (mod(x(i),100) == 0 ~= mod(x(i),400))
L(i)= L+0;
else (mod(x(i),4) == 0 ~=- mod(x(i),100))
L(i) = L+1
end
end

回答(1 个)

KSSV
KSSV 2022-1-27
编辑:KSSV 2022-1-27
You have not indexed L, so it was 2x1 vector and you are trying to save it into a single element which popped a error. Consider the below modified code.
x=1:1:2021;
L = zeros(2021,1) ;
L(1)=0 ;
for i = 2:2021
if mod(x(i),400) == 0
L(i)=L(i-1)+1;
elseif (mod(x(i),100) == 0 ~= mod(x(i),400))
L(i)= L(i-1)+0;
else (mod(x(i),4) == 0 ~=- mod(x(i),100));
L(i) = L(i-1)+1 ;
end
end

类别

Help CenterFile Exchange 中查找有关 Spectral Estimation 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by