7

I did a MATLAB code and I am trying to do it in C++ using Eigen library.In my MATLAB code I had to do a particular function like this

M=10;
s1 = zeros(20,M);
for i=1:M
  s1(:,i) = i*i;%some function 
  s1(:,i) = s1(:,i)/std(s1(:,i));
end

I am confused on using .colwise() and is there an in built function to get standard deviation using Eigen library?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Ema
  • 79
  • 1
  • 6

2 Answers2

12

As Yuyao pointed out, there is no standard deviation function built into Eigen (at the moment). You can compute this for a single vector using the following (generally, prefer using Array if you are working more on element-wise operations):

Eigen::ArrayXd vec;
double std_dev = std::sqrt((vec - vec.mean()).square().sum()/(vec.size()-1));

(Since there was an edit request: Note that for an un-biased estimation of the standard deviation you need to divide by vec.size()-1: [1])

If you want to compute the column-wise std-dev of a whole array, the following should work:

Eigen::Index N = 20, M = 10;
Eigen::ArrayXd angles = Eigen::ArrayXd::LinSpaced(N, -M_PI/2, M_PI/2);

Eigen::ArrayXXd s1(N, M);
for(Eigen::Index i=0; i< s1.cols(); ++i)
{
    s1.col(i) = (i+1)*sin(angles+i);
}

Eigen::Array<double, 1, Eigen::Dynamic> std_dev = ((s1.rowwise() - s1.colwise().mean()).square().colwise().sum()/(M-1)).sqrt();
std::cout << std_dev << "\n\n";

s1.rowwise() /= std_dev;

std::cout << s1 << "\n\n";
chtz
  • 17,329
  • 4
  • 26
  • 56
  • I don't think you need the -1 if `vec` represents the entire population (as opposed to a sampled sub-portion of it). This is because when you have the entire population, you'd have an exact calculation of the standard deviation, as opposed to an estimate with various biases that needs correcting. Because the OP doesn't specify either way, both solutions should be mentioned. – Arda Apr 17 '23 at 19:01
  • Since you are computing column-wise std, shouldn't you divide by `(N-1)` instead of `(M-1)`? Because you have N elements in a single column. – Tomingsun Jul 20 '23 at 07:36
0

I am confused on using .colwise()

You might want to use .col(i) instead.

Is there an in built function to get standard deviation using Eigen library?

No, since standard deviation function seems to be listed in this TODO List.

  • How to generate the C++ equivalent of MATLAB function std()? – Ema Mar 10 '18 at 09:12
  • I defined a matrix MatrixXcd s(181,3); and tried to initialise s.col(0)=sin(angles); //where angles are radian values of -90 to 90, it gave me an error. Is this command incomplete? – Ema Mar 10 '18 at 12:31