# Calculate mean of calendarDuration? (i.e. mean of age)

4 visualizzazioni (ultimi 30 giorni)
Max Gattie il 21 Gen 2019
Risposto: Eric Sofen il 8 Feb 2019
I have an array of calendarDuration data, formatted as (for example) 18y 5mo 21d.
The data is the ages of participants when I tested them. I subtracted their birth date from the testing date, using caldiff.
I'd like to calculate the mean age of my participants. The obvious step would be to do something like:
mean(ages)
Where ages is a calendarDuration array (e.g. a 1 x 33 calendarDuration array with the data I'm using).
When I try this, I get the error
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in mean (line 127)
y = sum(x, dim, flag) ./ mysize(x,dim);
What am I missing? For example, is there some different mean function I'm supposed to use for calendarDuration arrays? I couldn't find anything about this when I searched. The nearest I found was a description of timeseries means:
These use the built-in matlab 'mean' function. Which is about what I'd expect - adapting the built-in function so that it detects homogenous data type and operates accordingly would seem so useful that it would have been added long ago. But perhaps not for calendarDuration data, as per my example?
P.S. If anyone can tell me how to get the mean of calendarDuration data, could you tell me how to get standard deviation as well?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (1)

Eric Sofen il 8 Feb 2019
Hi Max,
You probably want to calculate the ages of the participants as durations, and mean() is defined for duration. calendarDuration is useful for representing elapsed time in calendar units so that vagaries like the number of days in a month get handled automatically. However, a lot of math on calendarDurations is ill-defined, because the length of a calendar duration isn't determined in terms of absolute time until the calendarDuration is "anchored" to a datetime (by adding it to that datetime). A calmonth could be anything from 28 to 31 days and with daylight savings time, a calday may be 23, 24, or 25 hours. Thus, what is mean([calmonths(3), caldays(5)])? calmonths and caldays are separate calendar units and you cannot do arithmetic combining them, which is why they're represented separately in your example 18y 5mo 21d.
Durations, in contrast, represent absolute elapsed time. Subtracting two datetimes (not using caldiff) gives a duration, and you can take the mean of durations.
>> births = datetime(1970,1,1)+10*years(randn(5,1))
births =
5×1 datetime array
14-Dec-1967 09:02:39
04-Oct-1968 13:43:47
24-Nov-1984 00:12:39
03-Feb-1984 09:25:37
04-Mar-1984 04:32:09
>> testDate = datetime+days(randn(5,1))
testDate =
5×1 datetime array
09-Feb-2019 07:37:30
07-Feb-2019 10:31:46
09-Feb-2019 08:43:22
10-Feb-2019 06:38:05
09-Feb-2019 03:14:33
>> meanAge = mean(testDate-births)
meanAge =
duration
360571:09:41
>> meanAge = years(mean(testDate-births))
meanAge =
41.1338
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### 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!

Translated by