Passing a matrix in a function () in C++

I`m a beginner of C++. Any help will be very appreciated!
I have an issue passing a matrix to a function in C++. There is the function i want to create:

#include <stdio.h>
#include <Eigen/Core>
#include <Eigen/Eigen>
#include<Eigen/Geometry>
using namespace Eigen;
using namespace Eigen::internal;
using namespace Eigen::Architecture;
using namespace std;

float matOpt(int nrows,int ncols,int Y[nrows][ncols],int X[nrows][ncols])
{
int tempMat1;
int tempMat2;
MatrixXf I(rows,cols);
I<<MatrixXf::Identity(nrows,nrows);
Yt<<Y.transpose();
Xt<<X.transpose();
Xinv<<X.inverse();
Yinv<<Y.inverse();
XtX=Xt*X;
tempMat1=X*XtX.inverse()*Xt;
tempMat2=Yt*(I-tempMat1)*Y;
return(tempMat2.determinant());
}


Last edited on
If you want to pass a multidimensional array, you must explicitly state its column size
1
2
3
#define COL_SIZE 5

float matOpt (int nrows, int Y[][COL_SIZE], int X[][COL_SIZE]);


So you can't use function parameters to specify the array size of subsequent function parameters. If your arrays are going to have a set number of columns, then you can hard-code them in. If not, you'll need to study up and use pointers.
Last edited on
Thank you for your advice. Every time i need to read different data, and the ncols of array is also different. And in my opinion, using pointers passes header elements address to the function rather than the whole matrix. And i need whole matrix to manipulate. So any other advice?
How did you end up with a C-style 2D array anyway? If you're using Eigen, why not take Eigen::MatrixXf to begin with?
Last edited on
I see what you mean, but how could i set X or Y matrix as the parameter of the function for matrix manipulation, if taking Eigen::MatrixXf to begin with?
And there is still something obscure between 2D array in C-style and matrix in Eigen.
Last edited on
Topic archived. No new replies allowed.