MATLAB: How to handle matrices in java

builder jajavaMATLAB Compiler SDKmatrices

[EDIT: 20110718 13:41 CDT – reformat – WDR]
I trying to call matlab function from java that takes two matrices as arguments.
I have to read the data file using java and after I have constructed the matrices (2 x 255 and 1 x 255) I call the matlab function compiled into java.
matlab:
data=load(L);
x = [data(:,1) data(:,2)];
y = data(:,3);
gp=gp_optim(gp,x,y); % gp is a structure
java:
data = main.readFile();
double[][] x = new double[255][2];
double[][] y = new double[255][1];
for (int i = 0; i < 255; i++) {
x[i][0] = data[i][0];
x[i][1] = data[i][1];
y[i][0] = data[i][2];
}
gpObj = li.gp_optim(1, gpObj[0], x, y);
Error:
com.mathworks.toolbox.javabuilder.MWException: Error using ==> chol
Matrix must be positive definite.
at com.mathworks.toolbox.javabuilder.internal.MWMCR.mclFeval(Native Method)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.access$600(MWMCR.java:23)
at com.mathworks.toolbox.javabuilder.internal.MWMCR$6.mclFeval(MWMCR.java:902)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at com.mathworks.toolbox.javabuilder.internal.MWMCR$5.invoke(MWMCR.java:800)
at $Proxy0.mclFeval(Unknown Source)
at com.mathworks.toolbox.javabuilder.internal.MWMCR.invoke(MWMCR.java:475)
at gpStuff.GpStuff.gp_optim(GpStuff.java:557)
at main.Main.main(Main.java:132)
So it seems that I don't call the matlab function correctly. How the matrices should be handled in java?

Best Answer

Hi,
the BUILDER JA comes with a javabuilder.jar (located in C:\Program Files\MATLAB\R2011a\toolbox\javabuilder\jar) which provides MATLAB datatypes, MWARRAY ( <- abstract class). Please look here for the example how to handle matrices:
Click on the getfactor.java to see the java code. For the MWARRAY class definition see here:
Related Question