使用valueForKeyPath查找NSMutableArray的平均值 [英] Find the average of an NSMutableArray with valueForKeyPath
问题描述
目前,我正试图找到一种简洁的方法来平均矩阵。显而易见的解决方案是对矩阵求和,然后除以元素的数量。但是,我在苹果开发者网站上遇到过一种方法,声称可以使用valueForKeyPath以更简单的方式完成此操作。这在以下链接:
Currently I'm trying to find a compact way to average a matrix. The obvious solution is to sum the matrix, then divide by the number of elements. I have, however, come across a method on the apple developer website that claims this can be done in a simpler way using valueForKeyPath. This is linked here:
以下是我正在努力尝试让它发挥作用的示例:
Here is the example I'm currently working on to try and get it to work:
-(void)arrayAverager
{
NSMutableArray *myArray = [NSMutableArray arrayWithCapacity:25];
[myArray addObject:[NSNumber numberWithInteger:myValue]];
NSNumber *averageValue = [myArray valueForKeyPath:@"@avg.self"];
NSLog(@"avg = %@", averageValue);
}
问题是:代替对数组进行平均,它只是打印出数组1中的元素。
The problem is: instead of averaging the array it merely prints out the elements in the array 1 by 1.
UPDATE
-(void) pixelAverager
{
//Put x-coordinate value of all wanted pixels into an array
NSMutableArray *xCoordinateArray = [NSMutableArray arrayWithCapacity:25];
[xCoordinateArray addObject:[NSNumber numberWithInteger:xCoordinate]];
NSLog(@"avg = %@", [xCoordinateArray valueForKeyPath:@"@avg.intValue"]);
}
推荐答案
你需要使用 @ avg.floatValue
(或 @ avg.doubleValue
,或者你有什么)。 @avg
运算符将平均点后面的名称指定的数组中对象的属性。文档在这一点上令人困惑,但这就是:
You need to use @avg.floatValue
(or @avg.doubleValue
, or what have you). The @avg
operator will average the property of the objects in the array specified by the name after the dot. The documentation is confusing on this point, but that is what:
获取由键路径$ b指定的属性指定的值运营商右边的$ b
to get the values specified by the property specified by the key path to the right of the operator
正在说。由于您拥有 NSNumber
对象的集合,因此您可以使用 * value
访问者之一,例如: floatValue
获取每个对象的值。例如:
Is saying. Since you have a collection of NSNumber
objects, you use one of the *value
accessors, e.g. floatValue
to get the value of each object. As an example:
#include <Foundation/Foundation.h>
int main(void) {
NSMutableArray *ma = [NSMutableArray array];
[ma addObject:[NSNumber numberWithFloat:1.0]];
[ma addObject:[NSNumber numberWithFloat:2.0]];
[ma addObject:[NSNumber numberWithFloat:3.0]];
NSLog(@"avg = %@", [ma valueForKeyPath:@"@avg.floatValue"]);
return 0;
}
编译并运行此代码会返回:
Compiling and running this code returns:
$ clang avg.m -framework Foundation -o avg
steve:~/code/tmp
$ ./avg
2013-01-18 12:33:15.500 avg[32190:707] avg = 2
steve:~/code/tmp
这种方法的好处在于,只要所有对象都响应指定的方法,这个工作适用于任何集合,同源或其他, @avg
会有效。
The nice thing about this approach is that this work for any collection, homogenous or otherwise, as long as all objects respond to the specified method, @avg
will work.
编辑
正如评论中所指出的, OP的问题在于他使用一个元素平均一个集合,因此它似乎只是打印集合的内容。对于 NSNumber
对象的集合, @ avg.self
的工作正常。
As pointed in the comments, the OP's problem is that he is averaging a collection with one element, and thus it appears to simply print the contents of the collection. For a collection of NSNumber
objects, @avg.self
works just fine.
这篇关于使用valueForKeyPath查找NSMutableArray的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!