fork download
  1. #include <iostream>
  2. #include <math.h>
  3. using namespace std;
  4. void correlate(int ny, int nx, const float* data, float* result)
  5. {
  6. float* inter = new float[ny*nx];
  7. for (int y = 0; y < ny; ++y)
  8. {
  9. double mean = 0.0;
  10. double sd = 0.0;
  11. double temp;
  12. //Finding the mean
  13. for (int x = 0; x < nx; ++x)
  14. {
  15. mean += data[x + y*nx];
  16. }
  17. mean = mean/nx;
  18. //cout<<mean<<endl;
  19. //Finding the Standard Deviation
  20. for (int x = 0; x < nx; ++x)
  21. {
  22. temp = data[x + y*nx] - mean;
  23. inter[x + y*nx] = temp;
  24. sd += (temp)*(temp);
  25. }
  26. sd= sqrt(sd/(nx-1));
  27. //cout<<nx<<" "<<sd<<endl;
  28. //Finding zero mean and unit variance
  29. for (int x = 0; x < nx; ++x)
  30. {
  31. inter[x + y*nx] = inter[x + y*nx] / sd;
  32. }
  33. }
  34. for (int i = 0; i < ny; i++)
  35. {
  36. for (int j = 0; j < ny; j++)
  37. {
  38. float sum = 0.0;
  39. for (int k = 0; k < nx; k++)
  40. sum = sum + inter[i * nx + k] * inter[j * nx + k];
  41. result[i * ny + j] = sum;
  42. }
  43. }
  44.  
  45. }
  46.  
  47.  
  48. int main() {
  49. float xarr[] = { 0, 2, 4, 6, 8 ,10};
  50. int nx = 3;
  51. int ny = 2;
  52. float *result = new float[ny*ny];
  53. correlate(2,3,xarr,result);
  54. for(int i = 0;i<ny*ny;i++)
  55. cout<< result[i] <<" ";
  56. return 0;
  57. }
Success #stdin #stdout 0.01s 5300KB
stdin
Standard input is empty
stdout
2 2 2 2