+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Member level 4
    Points: 1,119, Level: 7

    Join Date
    Apr 2010
    Location
    Mumbai, India
    Posts
    68
    Helped
    3 / 3
    Points
    1,119
    Level
    7

    error in matlab code for mean and standard deviation

    Hello all,
    I have written following matlab code for calculating the mean and standard deviation of x but I am getting an error for mean and standard deviation as NaN (Not a number). Kindly help. What is wrong with my code.

    Code:
    clc;
      close all;
      clear all;
      x =  [4.2426 + 3.1659i
         2.2956 + 2.3891i
        -2.2271 + 4.5607i
        -1.9326 - 3.1940i
         3.6840 + 3.0343i
         0.0677 + 2.9262i
        -2.9779 - 1.4531i
        -2.4633 + 3.2697i
        -3.9887 + 0.4685i
        -2.9322 - 2.2252i
        -4.3754 - 2.6187i
        -3.4865 - 3.4461i];
      sum_x = 0; 
      sum_x2 = 0;
      n=0;
          while x>=0
              n=n+1;
              sum_x=sum_x+x;
              sum_x2=sum_x2+x^2;
          end
          x_bar=sum_x/n;
          std_dev=sqrt((n*sum_x2-sum_x^2)/(n*(n-1)));

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 29,644, Level: 41
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Brazil
    Posts
    8,863
    Helped
    1115 / 1115
    Points
    29,644
    Level
    41
    Blog Entries
    9

    Re: error in matlab code for mean and standard deviation

    I noticed on above code at while statement that you perform evaluation of an imaginary array number on its argument. I presume it is expected an scalar one.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )



--[[ ]]--