I am trying to create a C mex file that will multiply an input Nx1 matrix by a constant Nx1 matrix. I want to create a matrix and assign it values that will always be the same for every call of the mex function. I have been looking at tutorials but all I can find are people creating matrices and tying them to either the input or output. My matrix should be completely independent of that.
Pseudo code of what I’m trying to do:
mxArray *input, *constant_matrix, *output;
input = mxDuplicateArray(prhs[0]);
constant_matrix = [10 15 20 73]; //<---- this is what I can't do.
for i = 1 to 4
output += input[i]*constant_matrix[i];
return output;
Thank you in advance!
If you want to create it on the stack, just do
..or if the matrix is a bit bigger, or you don’t know its size at compile time, you want to heap allocate. In a mex file use…
..and then populate it in a loop (where n is the number of elements). Do not forget to free the dynamically allocated memory if you use this second method.