mxArray *mxRotationT, *mxRotation;
double *Rotation;
mxRotation = mxCreateNumericMatrix(4, 4, mxDOUBLE_CLASS, mxREAL);
mxRotationT = mxCreateNumericMatrix(4, 4, mxDOUBLE_CLASS,mxREAL);
Rotation = mxGetPr(mxRotation);
(Rotation appears to be a 16x1 at this point right?)
//Load the MxArray
Rotation[0] = TFKin[0][0];
Rotation[1] = TFKin[0][1];
Rotation[2] = TFKin[0][2];
.... Rest of the elements, could be done in for loop.
mexCallMATLAB(1, &mxRotationT, 1, &mxRotation, "transpose" );
RotationT = mxGetPr(mxRotationT);
//Use loop to Rebuild new 4,4 Matrix from RotationT (16x1?)
Lastly, what is the processing penalty to doing the above with mexCallMATLAB, is it a fairly big hit processing wise to do MATLAB calls? This example could be done hard coded but your answers would also pertain to larger operations that could not be
Thanks,
Matt
The inputs & outputs need to be mxArrays.
> Also I have been trying to inderstand but are mxArrays always 1 Dimensional (or called single subscript multidimensional or something like that?)
mxArrays can be multidimensional. For inputs, use mxGetNumberOfDimensions and mxGetDimensions instead of mxGetM and mxGetN. For outputs, use mxCreateNumericArray, for example, instead of mxCreateNumericMatrix.
> mxArray *mxRotationT, *mxRotation;
> double *Rotation;
> mxRotation = mxCreateNumericMatrix(4, 4, mxDOUBLE_CLASS, mxREAL);
> mxRotationT = mxCreateNumericMatrix(4, 4, mxDOUBLE_CLASS,mxREAL);
> Rotation = mxGetPr(mxRotation);
> (Rotation appears to be a 16x1 at this point right?)
> //Load the MxArray
> Rotation[0] = TFKin[0][0];
> Rotation[1] = TFKin[0][1];
> Rotation[2] = TFKin[0][2];
> .... Rest of the elements, could be done in for loop.
> mexCallMATLAB(1, &mxRotationT, 1, &mxRotation, "transpose" );
You just created a memory leak here. mxRotationT already points to an mxArray, namely the one you created above. The mexCallMATLAB call will simply replace the pointer in mxRotationT with a new pointer to a newly created transpose result, without first destroying the current existing mxArray that it points to. Thus the memory leak. You were probably thinking that you need to somehow preallocate the result prior to the mexCallMATLAB call. You don't. Just let MATLAB create the result mxArrays for you.
> RotationT = mxGetPr(mxRotationT);
> //Use loop to Rebuild new 4,4 Matrix from RotationT (16x1?)
>
> Lastly, what is the processing penalty to doing the above with mexCallMATLAB, is it a fairly big hit processing wise to do MATLAB calls? This example could be done hard coded but your answers would also pertain to larger operations that could not be
Processing penalty for mexCallMATLAB itself is probably not very much, but for large matrices there would potentially be quite a bit of data movement involved that could slow things down quite a bit. For your small 4x4 problem it will definitely be faster to hand code the transpose yourself rather than building mxArrays and calling the transpose function through mexCallMATLAB. If you had a very large matrix to deal with, the MATLAB transpose function is quite fast and will very likely beat any hand coding you would do. For a general problem, there would have to be a trade between the faster MATLAB transpose function and the extra data movement. I don't know at what size the balance tips in favor of hand coding vs mexCallMATLAB.
James Tursa