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 Center および File ExchangeSpectral Estimation についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by