For fast an easy operations on matrices, you may consider to use std::valarray. This (not so popular) container has been desigend for exactly that purpose.
I admit that they are not so easy to understand, especially not the slice or gslice part, but if you learned how to use them, then they are very powerful.
And on some machines, they have native support and are extremely fast.
Here is one (of many) possible examples:
#include <iostream>
#include <valarray>
#include <numeric>
constexpr size_t NumberOfRows = 10;
constexpr size_t NumberOfColumns = 20;
constexpr size_t MatrixSize = NumberOfRows * NumberOfColumns;
int main(void)
{
// Define some valarrays
std::valarray<double> y(MatrixSize);
std::valarray<double> z(MatrixSize);
// And fill them with some demo values
std::iota(std::begin(y), std::end(y), 200);
std::iota(std::begin(z), std::end(z), 400);
// Perform mathematical operations on all elements of array
y /= 2.0;
z /= 2.0;
// Simply add 2 arrays to a new one
std::valarray<double> x = y + z;
// Display result
for (size_t i = 0; i < MatrixSize; ++i)
std::cout << (((i % NumberOfColumns) == 0) ? "\n" : " " ) << x[i];
return 0;
}
double ** x=new double*[1000];doesn't allocate a 2D array, it allocates a 1D array consisting of 1000double *pointers. Perhaps you intendeddouble x[1000][100];instead?x[i][j]=y[i][j]/2.0+z[i][j]/2.0;invokes Undefined Behavior asx[i]is an uninitialized pointer to nowhere. You would needfor(int i=0;i<1000;i++) { x[i] = new double[100]; for(int j=0;j<100;j++) ...