Fixed octave to java array and matrix conversions for size 1 arrays and matrices

This commit is contained in:
joseph.lizier 2012-11-02 08:21:59 +00:00
parent 734b2ac44a
commit 6557cd99c9
2 changed files with 27 additions and 4 deletions

View File

@ -8,8 +8,21 @@ function jDoubleArray = octaveToJavaDoubleArray(octaveArray)
if (exist ('OCTAVE_VERSION', 'builtin'))
% We're in octave:
% Using 'org.octave.Matrix' is much faster than conversion cell by cell
tmp = javaObject('org.octave.Matrix',octaveArray,[1, length(octaveArray)]);
jDoubleArray = tmp.asDoubleVector();
if (length(octaveArray) > 1)
% Do this the normal way
tmp = javaObject('org.octave.Matrix',octaveArray,[1, length(octaveArray)]);
jDoubleArray = tmp.asDoubleVector();
else
% For length 1 arrays, we need to perform a hack here or else
% java thinks the length-one array is a scalar.
% I thought I had this work once:
% tmp = javaObject('org.octave.Matrix',[octaveArray,octaveArray] ,[1, 1]);
% jDoubleArray = tmp.asDoubleVector();
% but now can't get that to repeat.
% So instead we'll do this the slow way (doesn't matter for one element only)
jDoubleArray = javaArray('java.lang.Double', 1);
jDoubleArray(1) = octaveArray(1);
end
else
% We're in matlab:

View File

@ -8,8 +8,18 @@ function jDoubleMatrix = octaveToJavaDoubleMatrix(octaveMatrix)
if (exist ('OCTAVE_VERSION', 'builtin'))
% We're in octave:
% Using 'org.octave.Matrix' is much faster than conversion cell by cell
tmp = javaObject('org.octave.Matrix',reshape(octaveMatrix,1,rows(octaveMatrix)*columns(octaveMatrix)),[rows(octaveMatrix), columns(octaveMatrix)]);
jDoubleMatrix = tmp.asDoubleMatrix();
if ((rows(octaveMatrix)*columns(octaveMatrix)) > 1)
% Do this the normal way
tmp = javaObject('org.octave.Matrix',reshape(octaveMatrix,1,rows(octaveMatrix)*columns(octaveMatrix)),[rows(octaveMatrix), columns(octaveMatrix)]);
jDoubleMatrix = tmp.asDoubleMatrix();
else
% For length 1 arrays, we need to perform a hack here or else
% java thinks the length-one array is a scalar.
% See octaveToJavaDoubleArray for a further description.
% So instead we'll do this the slow way (doesn't matter for one element only)
jDoubleMatrix = javaArray('java.lang.Double', 1, 1);
jDoubleMatrix(1, 1) = octaveMatrix(1);
end
else
% We're in matlab: