Re: Kmeans using mexCallMATLAB



"nils" <nilshomer@xxxxxxxxx> writes:

I want to perform kmeans clustering on an array of values that I pass
into the function below. MATLAB crashes during the call mexCallMATLAB
for kmeans. Any help would be greatly appreciated.

/* Takes in an array of values and performs kmeans clustering on the
values and stores the output in the mxOutput array */
void kmeans(int * values, int num_values, mxArray * mxOutput)
{
int num_input_args = 2;
int num_output_args = 1;
int i;
int colLen = 2;
int rowLen = num_values;
double * doubleValues;

mxArray * input_args[2];
mxArray * num_clusters;
mxArray * mxValues;

input_args[0] = mxValues;

^ Right here, mxValues is garbage. That garbage is copied to
input_args[0].


/* Set up the number of cluster to compute */
num_clusters = mxCreateNumericMatrix(1, 1, mxDOUBLE_CLASS, mxREAL);
(mxGetPr(num_clusters))[0] = NUM_KMEANS_CLUSTERS;
input_args[1] = num_clusters;

/* Set up the data to pass to kmeans */
mxValues = mxCreateDoubleMatrix(num_values, colLen, mxREAL);

^ Now you reassign mxValues to point to something real.

doubleValues = mxGetPr(mxValues);
for(i=0;i<rowLen;i++) {
doubleValues[i]= values[i];
doubleValues[i+rowLen] = 0;
}
mexCallMATLAB(num_output_args, &mxOutput, num_input_args,
input_args, "kmeans");

^ Now you call MATLAB with the input_args[0], which still has the
original garbage.



--
Peter Boettcher <boettcher@xxxxxxxxxx>
MIT Lincoln Laboratory
MATLAB FAQ: http://www.mit.edu/~pwb/cssm/
.



Relevant Pages

  • Re: int[][] from C-DLL in Matlab
    ... int CalculateMandelbrot(double posX, double posY, double width, double ... this is the wrong way to declare the array argument for matlab. ...
    (comp.soft-sys.matlab)
  • Re: Absolute beginner vs. fuzzy logic
    ... into B) is readily available in MatLab. ... new +1-dimensional array. ... The graph showed how a certain mechanical system ... > each iteration. ...
    (comp.soft-sys.matlab)
  • Re: zero based arrays?
    ... array, even in Matlab, its just that Matlab is an interpreted ... Every programming language has things it does differently from every other ... first) element of the array. ... case you'll be writing in C or C++ and so you'll use the native indexing ...
    (comp.soft-sys.matlab)
  • Re: need help on building a "Next" button in GUI
    ... I am a new user of MatLab. ... When I selected the pictures, they did form an array FileName(), but my next_callback did not work at all. ... % hObject handle to figure ... function varargout = Image_OutputFcn ...
    (comp.soft-sys.matlab)
  • new to FFTW - I have some questions
    ... I am new to using the FFTW algorithms in C++, ... I have used matlab for ... For example, the array might consist of 40 trials, ... optimal algorithm based on the size of the FFT alone? ...
    (comp.dsp)