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

4 views (last 30 days)
Andrei on 28 Dec 2022
Edited: George Abrahams on 1 Jan 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.
Voss on 29 Dec 2022

George Abrahams on 1 Jan 2023
Edited: George Abrahams on 1 Jan 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 , . 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 CommentsShowHide 1 older comment
Andrei on 1 Jan 2023

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.

### Categories

Find more on Data Type Conversion in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!