How to stop a for loop after comparing one value from an array with another value from another array?

1 回表示 (過去 30 日間)
c_year = [0 2 3 4 6 7 8 10];
l_year = [1 5 9];
count = 1188;
statement = true ;
for j = 1:length(l_year)
for i =1:length(c_year)
if c_year(i) >l_year(j)
count = count - j;
break
end
end
end
I want the loop to stop when the value in c_year is bigger than the value in l_year (e.g. stop when c_year value is 2 and l_year value is 1 and then take away 1 from count, or when c_year value is 6 and l_year value is 5 and take away 2 from count (because it is the 2nd value in l_year) or compare 10 and 9 and take away 3 from counter, I assume you get the idea).
Any suggestions how this can be done?
Also, sorry for the bad explanation, but I could not find a better way to explain the problem. Hope it is clear enough.
  13 件のコメント
Andrei
Andrei 2022 年 12 月 28 日
Yes, the wanted result for count is 1187 because in this case only 1 leap year has passed so count = count - 1.

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

採用された回答

George Abrahams
George Abrahams 2023 年 1 月 1 日
編集済み: George Abrahams 2023 年 1 月 1 日
Happy new year! What an appropriate time to answer this question... 😂
My understanding, from reading the comments, is that you want to calculate the duration in days between 2 dates, excluding leap days (the additional day in leap years). Specifically, between 1st Oct 1990 and the date corresponding to the maximum of an Mx2 matrix, data.
Presumably this is because you have some calendar data (i.e., it includes leap days) and some model which does not consider leap years, and you want to compare the 2. If so, potentially better solutions include:
  • Convert each data set matrix to a timetable with associated dates (array2timetable) and synchronize the two (synchronize with an interpolation method). See [1], [2]. Like this you can artificially add data for the leap days.
  • Remove the leap days (29th Feb) from the calandar data, see this answer and this answer. Then your count will just be your column number, i.e. 1:N. Just be aware that if you do this, most MATLAB built-in date/time functions will get confused, as "days" don't exist without leap years, only 24-hour periods exist.
  • You can continue to do it your way, calculating duration without leap days, but then you have some tricky decisions to make. Will you remove a day if you're currently in a leap year? What about if you're in a leap year either before 29 Feb or on 29 Feb? P.S. Instead of entering c_year and l_year manually, here's a neat little trick, courtesy of this answer:
isLeapYear = ~(datetime(1990:2000,2,29)==datetime(1990:2000,3,1))
isLeapYear = 1×11 logical array
0 0 1 0 0 0 1 0 0 0 1
EDIT: Code in the comments below.
  2 件のコメント
George Abrahams
George Abrahams 2023 年 1 月 1 日
To add some code to this, all 3 options in my reply above would start like this:
% Make random, dummy data (10000-by-2 matrix).
rng( 1, "twister" )
data = rand( 10000, 2 );
% Find the maximum data row, 'daysUntilMax', equivalent to the
% initial 'count' in your example.
[ ~, maxIdx ] = max( data, [], 'all' );
[ daysUntilMax, ~ ] = ind2sub( size(data), maxIdx );
% Convert data to a timetable.
data = array2timetable( data, ...
'StartTime', datetime(1990,10,1), 'TimeStep', caldays(1) );
Then, the following shows one way of doing option 3, which is precisely what you requested in your post.
% Count the number of leap years between the start date and date of
% the maximum. If either of these are in a leap year, that will also
% be subtracted, i.e. it is inclusive.
dataYears = year( data.Time( 1:daysUntilMax ) );
yearsBetweenDates = unique( dataYears );
isleapyear = @(yearNumbers) ~( datetime(yearNumbers,2,29) == ...
datetime(yearNumbers,3,1) );
leapYearsBetweenDates = sum( isleapyear(yearsBetweenDates) );
% Subtract number of leap years (similar but distinct from number of
% leap days) from 'daysUntilMax'. 'commonDaysUntilMax' is equivalent
% to the final 'count' in your example.
commonDaysUntilMax = daysUntilMax - leapYearsBetweenDates;
Andrei
Andrei 2023 年 1 月 1 日

Happy New Year! I will try this and then manipulate some values in the 'data' variable, corresponding with 1st Jan and 31st Dec each year, using the following link: https://www.timeanddate.com/date/durationresult.html?d1=1&m1=10&y1=1990&d2=31&m2=12&y2=1993&ti=on So index number 1188 would be the days between 1st Oct 1990 and 31st Dec 1993 and if max value occurs at index 1188 then the output date would be 31st Dec 1993.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by