4.33333

4.3 | 3 ratings Rate this file 29 downloads (last 30 days) File Size: 1.8 KB File ID: #14172

ISLEAP Function

by Giuseppe Cardillo

 

06 Mar 2007 (Updated 23 Dec 2009)

Code covered by the BSD License  

Calculate leap year

Download Now | Watch this File

File Information
Description

ISLEAP returns 1 if Year is a leap year and 0 otherwise. ISLEAP is only set for gregorian calendar, so Year >= 1583.

The Gregorian calendar has 97 leap years every 400 years:
Every year divisible by 4 is a leap year.
However, every year divisible by 100 is not a leap year.
However, every year divisible by 400 is a leap year after all.
So, 1700, 1800, 1900, 2100, and 2200 are not leap years, but 1600, 2000, and 2400 are leap years.

You can find more details on:
http://www.advancedmcode.org/isleap.html

You can visit my homepage http://home.tele2.it/cardillo
My profile on XING http://www.xing.com/go/invita/13675097
My profile on LinkedIN http://it.linkedin.com/in/giuseppecardillo

MATLAB release MATLAB 6.5 (R13)
Zip File Content  
Other Files isleap.m,
license.txt
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
17 Sep 2007 Marc FC  
20 Jan 2009 Vince Petaccio

Cool, just what I needed. Thanks! :o]

05 Oct 2009 Jan Simon

CLOCK -> DATE -> DATESTR -> STR2DOUBLE waste a lot of time: str2double(datestr(date,10))
Faster: c = clock; default = {c(1)};

Checking the number of inputs can be done easier and faster without VARARGIN, e.g.:
  function x = isleap(Year)
  switch nargin
  case 1, % Your error checks...
  case 0, c = clock; Year = c(1);
  otherwise error(...);
  end

The permanent disabling of the warning for logical conversion is not secure. You can omit this by simplifying the engine - instead of:
  warning('OFF','MATLAB:conversionToLogical')
  A=logical(mod(Year,4));
  B=logical(mod(Year,100));
  C=logical(mod(Year,400));
  x=ismember(Year,Year(~C | (~A & B)));
call just:
  x = ~mod(Year, 4) & (mod(Year, 100) | ~mod(Year, 400));
And suddenly the function is 150 times faster (400 times if called without argument).
Good help, H1-line, date, author and reference included. I'll rate this, if its efficiency is encreased.

14 Oct 2009 Jan Simon

Descriptive help, reliable error checking, new version is really efficient, does exactly what the name says.

Please login to add a comment or rating.
Updates
11 Dec 2007

more elegant use of logical operators

11 Dec 2007

another logial indexing improvement

24 Sep 2008

Improvement in input error handling

12 Nov 2008

Changes in help section

05 Oct 2009

Change in description

13 Oct 2009

Modified after Simon Jan comment (also added into the help lines)

23 Dec 2009

Changes in description

Tag Activity for this File
Tag Applied By Date/Time
clock Giuseppe Cardillo 22 Oct 2008 09:02:49
fun Giuseppe Cardillo 22 Oct 2008 09:02:49
leap year Giuseppe Cardillo 22 Oct 2008 09:02:49
utilities Giuseppe Cardillo 22 Oct 2008 09:02:49
gregorian calendar Giuseppe Cardillo 22 Oct 2008 09:02:49
calculates Giuseppe Cardillo 22 Oct 2008 09:02:49
utilities Cristina McIntire 12 Nov 2008 13:57:19
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com