<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def within_duration(events, time, limits):
 """Check whether event is within time limits. Parameters events : ndarray (dtype='int') N x M matrix with start sample first and end samples last on M time : ndarray (dtype='float') vector with time points limits : tuple of float low and high limit for spindle duration Returns ------- ndarray (dtype='int') N x M matrix with start sample first and end samples last on M """

    min_dur = max_dur = ones(events.shape[0], dtype=bool)
    
    if limits[0] is not None:
        min_dur = time[events[:, -1] - 1] - time[events[:, 0]] >= limits[0]
    
    if limits[1] is not None:
        max_dur = time[events[:, -1] - 1] - time[events[:, 0]] <= limits[1]

    return events[min_dur & max_dur, :]