MATLAB: How to – “sort” using mexCallMATLAB

embedded matlabmex-functionmexcallmatlabsort

Dear all,
I am writing a mex-function and need to sort some data using the mexCallMATLAB API so that I get the data in the required form. i.e I need the data in descending order along with the index values.
In matlab – [y,i] = sort(x,1,'descend'); does the job for me. Can I simulate the same through the mexCallMATLAB API? If yes, how does it fit in the syntax for the API?
Should I write a C-code for the same?
Thanks in advance

Best Answer

  • Using a quicksort in C directly might be more efficient. Especially if your data set is small, the overhead of calling Matlab will be important. For large data sets it matters, that Matlab's SORT is highly efficient and mexCallMATLAB could be the best choice:
    mxArray *In[3];
    mxArray *Out[2];
    In[0] = <your data>;
    In[1] = mxCreateDoubleScalar(1.0);
    In[2] = mxCreateString("descend");
    mexCallMATLAB(2, Out, 3, In, "sort");