This should be an easy question! I want to define a function with domain = the 12 integers {1,12}, with the values f[1]=31, f[2]=28, f[3]=31, etc. (number of days in the month). This will be a part of nested Do[] loops running through the days of a non-leap year for a particular data set I am working with.
|
|
||||
|
Try
|
|||
|
|
Another approach:
One could also do the calculation at run time:
Or with memoization:
|
||||
|
|
|
Here's a variation of Brett's and celtschk's answers, which uses
|
|||
|
|
|
I advise against using Here is the
Here is the same thing avoiding
Here is direct definition for each value (
Here is a related operation using a
|
|||||
|
|
Here's a solution making use of the fact that you can assign to a list of variables:
although with this approach you can only do it once. (Otherwise the |
|||
|
|
This is probably the simplest way to define it:
|
|||||||||||
|
|
|
How about this, which takes the name of the variable (eg
eg, for 2011 (which was not leap)
and then eg
gives
takes into account the fact that 2008 was leap. EDIT: Note that this defines |
||||
|



f[m,d] = <hours per day>. Who'd want to type all 365(6) entries in by hand? – Brett Champion Mar 5 '12 at 18:40