フィルターのクリア

need to compile outputs from for loop into array

24 ビュー (過去 30 日間)
Lewis
Lewis 2024 年 4 月 16 日 18:11
編集済み: Torsten 2024 年 4 月 17 日 0:24
startYear = input('Pick a year to start the Array from. \n');
endYear = input('Pick a year to end the Array at. \n');
ctr=1;
yeararray = startYear:endYear;
outputarray = []
for n = yeararray
if IsLeapYear(n) == "True"
outputarray(ctr) = outputarray(ctr) + n;
ctr = ctr + 1;
end
end
disp(outputarray)
A couple pieces of code are missing from this but i think you can understand the process, im trying to grab Leap Years in between 2 points and compile them into and Array but are struggling to do so. I tried to compile them into an array using numel():
outputarray = zeros(1, numel(yeararray))
however this makes the array way too big for the amount of values it outputs and just provides me with a bunch of zeros that are filler. Any tips?

採用された回答

Torsten
Torsten 2024 年 4 月 16 日 18:46
移動済み: Torsten 2024 年 4 月 16 日 18:46
In most cases,
outputarray = zeros(1,nnz(mod(yeararray,4)==0))
will work.
  3 件のコメント
Lewis
Lewis 2024 年 4 月 16 日 19:33
so this doesnt work for higher ranges, any other tips for that?
i did 1:1000 and it had 0's past 996
Torsten
Torsten 2024 年 4 月 17 日 0:21
編集済み: Torsten 2024 年 4 月 17 日 0:24
I wrote in most cases. The exact rule is
outputarray = zeros(1,nnz( (mod(yeararray,4) == 0 & mod(yeararray,100) ~= 0) | (mod(yeararray,400) == 0)))
Since you now know the number of leapyears within the yeararray, you can substitute your complete code by a one-liner.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by