function [Lambda,V]=My_Eig(cov)
[v,w] = eig(cov);
[Lambda, ind]=sort(diag(w),'descend');
V=v(:,ind);
end
this is my code for fixed size variables in Visual Studio
// This is the main DLL file.
#include "stdafx.h"
#include <array>
#include "Eigenvalue.h"
#include <My_Eig.h>
void Eigenvalue::Class1::Eigen(array<array<double>^>^ input, array<double, 2>^out_Lambda, array<double, 2>^ out_V, int cols)
{
My_Eig_initialize();
const int cov_length = cols * cols;
const int lamda_length = cols;
real_T *Cov = new real_T[cols * cols];
creal_T *Lambda = new creal_T[cols];
creal_T *V = new creal_T[cols * cols];
int c = 0;
// Copy values from input to Cov matrix
for (int i = 0; i < cols; i++)
{
for (int j = 0; j < cols; j++)
{
Cov[c] = input[i][j];
c++;
}
}
// Call My_Eig function to retrieve Lambda and V matrices
My_Eig(Cov, Lambda, V);
// Copy values from Lamda and V to out_Lambda and out_V
for (int i = 0; i < cols; i++)
{
out_Lambda[i,0] = Lambda[i].re;
// out_Lambda[i,1] = Lambda[i].im;
}
for (int i = 0; i < (cols * cols); i++)
{
out_V[i, 0] = V[i].re;
// out_V[i, 1] = V[i].im;
}
My_Eig_terminate();
return;
}
and this is the code generated by matlab
10 #ifndef __MY_EIG_H__
11 #define __MY_EIG_H__
12 /* Include files */
13 #include <math.h>
14 #include <stddef.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "rtwtypes.h"
19 #include "My_Eig_types.h"
20
21 /* Function Declarations */
22 void My_Eig(const emxArray_real_T *cov, emxArray_creal_T *Lambda, emxArray_creal_T *V);
23 void My_Eig_initialize();
24 void My_Eig_terminate();
25 emxArray_creal_T *emxCreateND_creal_T(int32_T numDimensions, int32_T *size);
26 emxArray_real_T *emxCreateND_real_T(int32_T numDimensions, int32_T *size);
27 emxArray_creal_T *emxCreateWrapperND_creal_T(creal_T *data, int32_T numDimensions, int32_T *size);
28 emxArray_real_T *emxCreateWrapperND_real_T(real_T *data, int32_T numDimensions, int32_T *size);
29 emxArray_creal_T *emxCreateWrapper_creal_T(creal_T *data, int32_T rows, int32_T cols);
30 emxArray_real_T *emxCreateWrapper_real_T(real_T *data, int32_T rows, int32_T cols);
31 emxArray_creal_T *emxCreate_creal_T(int32_T rows, int32_T cols);
32 emxArray_real_T *emxCreate_real_T(int32_T rows, int32_T cols);
33 void emxDestroyArray_creal_T(emxArray_creal_T *emxArray);
34 void emxDestroyArray_real_T(emxArray_real_T *emxArray);
35 #endif
36 /* End of code generation (My_Eig.h) */