Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9195793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:42:25+00:00 2026-06-17T21:42:25+00:00

Currently I’m trying to find a compact way to average a matrix . The

  • 0

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:

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/KeyValueCoding/Articles/CollectionOperators.html

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);

} 

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"]);
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:42:26+00:00Added an answer on June 17, 2026 at 9:42 pm

    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:

    to get the values specified by the property specified by the key path
    to the right of the operator

    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
    

    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.

    EDIT

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently I have 2 ways of displaying images in a cell, which way will
Currently I am trying to use a bunch of custom perl modules, test.pm as
Currently i have a list of sortable elements with a text: How can i
Currently I am loooking for a way to develop an algorithm which is supposed
Currently I'm trying to parse some html and return an array with the values
Currently if i have dependency project opened, then maven use it instead of specified
Currently using Oracle/BEA/Plumtree ALUI Portal 6.1, and am trying to display a client's IP
currently I am trying to replace a simple HTML element to alert the user
Currently I am trying to make my app available for iOS4, to increase the
Currently I am trying to change the icon of a particular feature of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.