From 6557cd99c98b54bc7e3e7efc1d7267f28571e4b5 Mon Sep 17 00:00:00 2001 From: "joseph.lizier" Date: Fri, 2 Nov 2012 08:21:59 +0000 Subject: [PATCH] Fixed octave to java array and matrix conversions for size 1 arrays and matrices --- demos/octave/octaveToJavaDoubleArray.m | 17 +++++++++++++++-- demos/octave/octaveToJavaDoubleMatrix.m | 14 ++++++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/demos/octave/octaveToJavaDoubleArray.m b/demos/octave/octaveToJavaDoubleArray.m index 94c84c5..b4d08f7 100755 --- a/demos/octave/octaveToJavaDoubleArray.m +++ b/demos/octave/octaveToJavaDoubleArray.m @@ -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: diff --git a/demos/octave/octaveToJavaDoubleMatrix.m b/demos/octave/octaveToJavaDoubleMatrix.m index 67fd3b2..08d9cdc 100755 --- a/demos/octave/octaveToJavaDoubleMatrix.m +++ b/demos/octave/octaveToJavaDoubleMatrix.m @@ -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: