How to handle matrices in java
4 次查看(过去 30 天)
显示 更早的评论
[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?
0 个评论
采纳的回答
Friedrich
2011-7-19
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:
更多回答(2 个)
Titus Edelhofer
2011-7-18
Hi,
is this the MATLAB code?
data=load(L);
x = [data(:,1) data(:,2)];
y = data(:,3);
This will not work, since data will be a structure... But besides this: one easy way to find out what's going on: put into the first line of your MATLAB code some
if isdeployed
save c:\temp\inputdata.mat
end
Then compile and run, and in MATLAB you can do:
load inputdata
% call your function in debug mode ...
Titus
2 个评论
Walter Roberson
2011-7-18
data will not be a structure if what is being load()'d is a text file. As the documentation indicates,
S = load(filename) loads the variables from a MAT-file into a structure array, or data from an ASCII file into a double-precision array.
Titus Edelhofer
2011-7-18
Hi Walter,
yes of course, it could be an ascii file, didn't think of this ...
Titus
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Java Package Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!