How to stop a for loop after comparing one value from an array with another value from another array?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Andrei
il 28 Dic 2022
Modificato: George Abrahams
il 1 Gen 2023
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.
Risposta accettata
George Abrahams
il 1 Gen 2023
Modificato: George Abrahams
il 1 Gen 2023
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))
EDIT: Code in the comments below.
2 Commenti
George Abrahams
il 1 Gen 2023
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;
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Calendar in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!