Thread: Mean, Variance, Squaring

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    (a - b) and I would like it squared, is there an equivalent to Matlab's
    (a - b)^2?
    The C math library (link) has some stuff like that. Nothing terribly special though - just all the basic functions (e^x, log, pow, trig, etc). So to answer your question:
    Code:
    #include <math.h> //#include <cmath> instead for C++ environments
    ...
    
    double ans = pow(a, b); //double = a^b
    but IMO it's just easier to write out the multiplication for squares.

    sqrt( (a-mean)*(a-mean) + (b-mean)* (b-mean) + ... + (n-mean)*(n-mean))/n)
    You could instead add the numbers in a loop. So instead of using variables a-n, you have an array of numbers.
    Code:
    double numbers[n];
    //fill array with values here: numbers[0]=3, numbers[1] =6, etc.
    
    double sum=0;
    int i;
    for (i=0; i<n; i++) {
        sum+=(numbers[i]-mean)*(numbers[i]-mean);
    }
    
    double answer = sqrt(sum / n);
    Consider this post signed

  2. #2
    Registered User
    Join Date
    Sep 2010
    Location
    London
    Posts
    41
    Thank you very much, yes I've used the last idea you suggested. It is looking much neater that way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Squaring numbers in c++
    By Sanka792 in forum C++ Programming
    Replies: 13
    Last Post: 01-27-2009, 02:03 PM
  2. Calculating Variance
    By Phyxashun in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2009, 05:28 AM
  3. calculating the variance of random numbers
    By Unregistered in forum C Programming
    Replies: 18
    Last Post: 11-22-2004, 08:16 AM
  4. variance
    By jerrysmith in forum C Programming
    Replies: 2
    Last Post: 12-23-2003, 04:59 PM
  5. mean and variance
    By tmoney$ in forum C Programming
    Replies: 2
    Last Post: 05-06-2003, 01:46 PM

Tags for this Thread