13

我正在尝试计算 NSArray 中一组(小)NSNumber 的中位数。NSArray 中的每个对象都是一个 NSNumber。

这是我正在尝试的,但它不起作用:

NSNumber *median = [smallNSArray valueForKeyPath:@"@median.floatValue"];
4

3 回答 3

24
NSArray *sorted = [smallNSArray sortedArrayUsingSelector:@selector(compare:)];    // Sort the array by value
NSUInteger middle = [sorted count] / 2;                                           // Find the index of the middle element
NSNumber *median = [sorted objectAtIndex:middle];                                   // Get the middle element

你可以变得更花哨。例如,具有偶数个数字的集合的中位数在技术上是中间两个数字的平均值。你也可以把它包装成一个简洁的单行方法,放在一个类别中NSArray

@interface NSArray (Statistics)
- (id)median;
@end

@implementation NSArray (Statistics)

- (id)median
{
    return [[self sortedArrayUsingSelector:@selector(compare:)] objectAtIndex:[self count] / 2];
}

@end
于 2010-07-14T20:48:29.963 回答
6

对于任何对此函数有特殊需求的人,这里有一个 NSArray 上的类别方法,它可以处理奇数和偶数个元素:

NSARRAY 类别方法

- (float)median {
    if (self.count == 1) return [self[0] floatValue];

    float result = 0;
    NSUInteger middle;

    NSArray * sorted = [self sortedArrayUsingSelector:@selector(compare:)];
    if (self.count % 2 != 0) {  //odd number of members
        middle = (sorted.count / 2);
        result = [[sorted objectAtIndex:middle] floatValue];
    }
    else {
        middle = (sorted.count / 2) - 1;
        result = [[@[[sorted objectAtIndex:middle], [sorted objectAtIndex:middle + 1]] valueForKeyPath:@"@avg.self"] floatValue];
    }
    return result;
}

测试

NSArray * singleElement = @[@1];
NSArray * oddNumberOfElements = @[@3, @5, @7, @12, @13, @14, @19, @20, @21, @22, @23, @29, @39, @40, @56];
NSArray * evenNumberOfElements = @[@3, @5, @7, @12, @13, @14, @19, @20, @21, @22, @23, @29, @40, @56];

NSLog(
    @"oddNumberOfElements: %f, evenNumberOfElements: %f singleElement: %f",
    [oddNumberOfElements median], [evenNumberOfElements median], [singleElement median]
);
//oddNumberOfElements: 20.000000, evenNumberOfElements: 19.500000 singleElement: 1.000000
于 2014-03-24T03:48:36.480 回答
3

快速扩展

extension Array where Element: Comparable {

  var median: Element {
    return self.sort(<)[self.count / 2]
  }

}
于 2015-07-28T13:43:33.657 回答