-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathoctaveToJavaIntMatrix.m
executable file
·58 lines (51 loc) · 2.21 KB
/
octaveToJavaIntMatrix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
%%
%% Java Information Dynamics Toolkit (JIDT)
%% Copyright (C) 2012, Joseph T. Lizier
%%
%% This program is free software: you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published by
%% the Free Software Foundation, either version 3 of the License, or
%% (at your option) any later version.
%%
%% This program is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%% GNU General Public License for more details.
%%
%% You should have received a copy of the GNU General Public License
%% along with this program. If not, see <http://www.gnu.org/licenses/>.
%%
% function jIntMatrix = octaveToJavaIntMatrix(octaveMatrix)
%
% Convert a native octave matrix to a java int 2D array.
%
% Assumes the JIDT jar is already on the java classpath - you will get a
% java classpath error if this is not the case.
%
function jIntMatrix = octaveToJavaIntMatrix(octaveMatrix)
if (exist ('OCTAVE_VERSION', 'builtin'))
% We're in octave:
% Using 'org.octave.Matrix' is much faster than conversion cell by cell
if ((rows(octaveMatrix)*columns(octaveMatrix)) > 1)
% Do this the normal way
tmp = javaObject('infodynamics.utils.OctaveMatrix');
try
tmp.loadIntData(reshape(octaveMatrix,1,rows(octaveMatrix)*columns(octaveMatrix)),[rows(octaveMatrix), columns(octaveMatrix)]);
catch
% Most likely error here is that octaveMatrix is interpreted as booleans, so try loading as booleans:
tmp.loadBooleanAsIntData(reshape(octaveMatrix,1,rows(octaveMatrix)*columns(octaveMatrix)),[rows(octaveMatrix), columns(octaveMatrix)]);
end
jIntMatrix = tmp.asIntMatrix();
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)
jIntMatrix = javaArray('java.lang.Integer', 1, 1);
jIntMatrix(1, 1) = octaveMatrix(1);
end
else
% We're in matlab: the native matlab 2D array can be passed to java as is:
jIntMatrix = octaveMatrix;
end
end