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

1 view (last 30 days)
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

Answers (1)

KSSV
KSSV on 27 Jan 2022
Edited: KSSV on 27 Jan 2022
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

Tags

Products

Community Treasure Hunt

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

Start Hunting!