Skip to content

Commit efccae1

Browse files
committed
Adding checks to ensure MATLAB inputs are doubles.
1 parent fbfc35e commit efccae1

File tree

9 files changed

+66
-18
lines changed

9 files changed

+66
-18
lines changed

MIToolbox.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
%Mutual Information = 7
2727
%Conditional MI = 8
2828

29+
for i = 1:length(varargin)
30+
if (~isa(varargin{i},'double'))
31+
error('Error, MIToolbox requires inputs to be double vector or matrices')
32+
end
33+
end
34+
2935
if (strcmpi(functionName,'Joint') || strcmpi(functionName,'Merge'))
3036
[varargout{1}] = MIToolboxMex(3,varargin{1});
3137
elseif (strcmpi(functionName,'Entropy') || strcmpi(functionName,'h'))

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ These functions are targeted for use with feature selection algorithms rather
1414
than communication channels and so expect all the data to be available before
1515
execution and sample their own probability distributions from the data.
1616

17+
All functions expect the inputs to be vectors or matrices of doubles.
18+
1719
Functions contained:
1820
- Entropy
1921
- Conditional Entropy
@@ -33,33 +35,39 @@ Note: all functions are calculated in log base 2, so return units of "bits".
3335

3436
Examples:
3537

38+
```
3639
>> y = [1 1 1 0 0]';
3740
>> x = [1 0 1 1 0]';
38-
41+
```
42+
```
3943
>> mi(x,y) %% mutual information I(X;Y)
4044
ans =
4145
0.0200
42-
46+
```
47+
```
4348
>> h(x) %% entropy H(X)
4449
ans =
4550
0.9710
46-
51+
```
52+
```
4753
>> condh(x,y) %% conditional entropy H(X|Y)
4854
ans =
4955
0.9510
50-
56+
```
57+
```
5158
>> h( [x,y] ) %% joint entropy H(X,Y)
5259
ans =
5360
1.9219
54-
61+
```
62+
```
5563
>> joint([x,y]) %% joint random variable XY
5664
ans =
5765
1
5866
2
5967
1
6068
3
6169
4
62-
70+
```
6371
======
6472

6573
To compile the library for use in MATLAB/OCTAVE, execute CompileMIToolbox.m
@@ -72,6 +80,7 @@ install MIToolbox into /usr/local/lib & /usr/local/include.
7280
All code is licensed under the 3-clause BSD license.
7381

7482
Update History
83+
- 10/01/2016 - v2.1.2 - Relicense from LGPL to BSD. Added checks to ensure input MATLAB types are doubles.
7584
- 02/02/2015 - v2.1.1 - Fixed up the Makefile so it installs the headers too.
7685
- 22/02/2014 - v2.1 - Fixed a couple of bugs related to memory handling.
7786
Added a make install for compatibility with PyFeast.

RenyiMIToolbox.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
%Renyi Entropy = 1;
1818
%Renyi MI = 3;
1919

20+
for i = 1:length(varargin)
21+
if (~isa(varargin{i},'double'))
22+
error('Error, MIToolbox requires inputs to be double vector or matrices')
23+
end
24+
end
25+
2026
if (alpha ~= 1)
2127
if (strcmpi(functionName,'Entropy') || strcmpi(functionName,'h'))
2228
%disp('Calculating Entropy');

WeightedMIToolbox.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
%Mutual Information = 4
2424
%Conditional MI = 5
2525

26+
for i = 1:length(varargin)
27+
if (~isa(varargin{i},'double'))
28+
error('Error, MIToolbox requires inputs to be double vector or matrices')
29+
end
30+
end
31+
2632
if (strcmpi(functionName,'Entropy') || strcmpi(functionName,'h'))
2733
%disp('Calculating Entropy');
2834
if (size(varargin{1},2)>1)

cmi.m

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,29 @@
88
%returns the mutual information between X and Y conditioned on Z, I(X;Y|Z)
99

1010
if nargin == 3
11+
if (~isa(X,'double') || ~isa(Y,'double') || ~isa(Z,'double'))
12+
error('Error, inputs must be double vectors or matrices')
13+
end
1114
if (size(X,2)>1)
12-
mergedFirst = MIToolboxMex(3,X);
15+
mergedFirst = MIToolboxMex(3,X);
1316
else
14-
mergedFirst = X;
17+
mergedFirst = X;
1518
end
1619
if (size(Y,2)>1)
17-
mergedSecond = MIToolboxMex(3,Y);
20+
mergedSecond = MIToolboxMex(3,Y);
1821
else
19-
mergedSecond = Y;
22+
mergedSecond = Y;
2023
end
2124
if (size(Z,2)>1)
22-
mergedThird = MIToolboxMex(3,Z);
25+
mergedThird = MIToolboxMex(3,Z);
2326
else
24-
mergedThird = Z;
27+
mergedThird = Z;
2528
end
2629
[output] = MIToolboxMex(8,mergedFirst,mergedSecond,mergedThird);
2730
elseif nargin == 2
31+
if (~isa(X,'double') || ~isa(Y,'double'))
32+
error('Error, inputs must be double vectors or matrices')
33+
end
2834
output = mi(X,Y);
2935
else
3036
output = 0;

condh.m

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,24 @@
88
%returns the conditional entropy of X given Y, H(X|Y)
99

1010
if nargin == 2
11+
if (~isa(X,'double') || ~isa(Y,'double'))
12+
error('Error, inputs must be double vectors or matrices')
13+
end
1114
if (size(X,2)>1)
12-
mergedFirst = MIToolboxMex(3,X);
15+
mergedFirst = MIToolboxMex(3,X);
1316
else
14-
mergedFirst = X;
17+
mergedFirst = X;
1518
end
1619
if (size(Y,2)>1)
17-
mergedSecond = MIToolboxMex(3,Y);
20+
mergedSecond = MIToolboxMex(3,Y);
1821
else
19-
mergedSecond = Y;
22+
mergedSecond = Y;
2023
end
2124
[output] = MIToolboxMex(6,mergedFirst,mergedSecond);
2225
elseif nargin == 1
26+
if (~isa(X,'double'))
27+
error('Error, inputs must be double vectors or matrices')
28+
end
2329
output = h(X);
2430
else
2531
output = 0;

h.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
%
66
%returns the entropy of X, H(X)
77

8+
if (~isa(X,'double'))
9+
error('Error, inputs must be double vectors or matrices')
10+
end
811
if (size(X,2)>1)
9-
mergedVector = MIToolboxMex(3,X);
12+
mergedVector = MIToolboxMex(3,X);
1013
else
11-
mergedVector = X;
14+
mergedVector = X;
1215
end
1316
[output] = MIToolboxMex(4,mergedVector);

joint.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
%if the joint variable is only compared with variables using the same samples,
1010
%then arity information is not required
1111

12+
if (~isa(X,'double') || ~isa(arities,'double'))
13+
error('Error, inputs must be double vectors or matrices')
14+
end
1215
if (nargin == 2)
1316
[output] = MIToolboxMex(3,X,arities);
1417
else

mi.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
%
88
%returns the mutual information between X and Y, I(X;Y)
99

10+
if (~isa(X,'double') || ~isa(Y,'double'))
11+
error('Error, inputs must be double vectors or matrices')
12+
end
1013
if (size(X,2)>1)
1114
mergedFirst = MIToolboxMex(3,X);
1215
else

0 commit comments

Comments
 (0)