3131
3232/**
3333 * @author Paul
34- * @version $Id$
3534 */
3635public class MatrixUtil {
3736
3837 /**
39- * @param d
40- * @return true if any of the values are very close to zero.
38+ * @param d
39+ * @return true if any of the values are very close to zero.
4140 */
4241 public static boolean containsNearlyZeros ( DoubleMatrix1D d ) {
4342 for ( int i = 0 ; i < d .size (); i ++ ) {
@@ -49,7 +48,7 @@ public static boolean containsNearlyZeros( DoubleMatrix1D d ) {
4948 /**
5049 * Extract the diagonal from a matrix.
5150 *
52- * @param matrix
51+ * @param matrix
5352 * @return
5453 */
5554 public static DoubleMatrix1D diagonal ( DoubleMatrix2D matrix ) {
@@ -63,8 +62,8 @@ public static DoubleMatrix1D diagonal( DoubleMatrix2D matrix ) {
6362 }
6463
6564 /**
66- * @param n
67- * @param indexToDrop
65+ * @param n
66+ * @param indexToDrop
6867 * @return
6968 */
7069 public static DoubleMatrix2D dropColumn ( DoubleMatrix2D n , int indexToDrop ) {
@@ -108,7 +107,7 @@ public static DoubleMatrix2D dropColumns( DoubleMatrix2D n, Collection<Integer>
108107 /**
109108 * Makes a copy
110109 *
111- * @param list
110+ * @param list
112111 * @return
113112 */
114113 public static DoubleMatrix1D fromList ( DoubleArrayList list ) {
@@ -120,12 +119,12 @@ public static DoubleMatrix1D fromList( DoubleArrayList list ) {
120119 }
121120
122121 /**
123- * @param <R>
124- * @param <C>
125- * @param <V>
126- * @param matrix
127- * @param rowIndex
128- * @param colIndex
122+ * @param <R>
123+ * @param <C>
124+ * @param <V>
125+ * @param matrix
126+ * @param rowIndex
127+ * @param colIndex
129128 * @return
130129 */
131130 public static <R , C , V > V getObject ( Matrix2D <R , C , V > matrix , int rowIndex , int colIndex ) {
@@ -139,11 +138,11 @@ public static <R, C, V> V getObject( Matrix2D<R, C, V> matrix, int rowIndex, int
139138 }
140139
141140 /**
142- * @param <R>
143- * @param <C>
144- * @param <V>
145- * @param matrix
146- * @param rowIndex
141+ * @param <R>
142+ * @param <C>
143+ * @param <V>
144+ * @param matrix
145+ * @param rowIndex
147146 * @return
148147 */
149148 public static <R , C , V > V [] getRow ( Matrix2D <R , C , V > matrix , int rowIndex ) {
@@ -178,8 +177,8 @@ public static DoubleMatrix1D multWithMissing( DoubleMatrix1D a, DoubleMatrix2D b
178177 }
179178
180179 /**
181- * @param a
182- * @param b
180+ * @param a
181+ * @param b
183182 * @return
184183 */
185184 public static DoubleMatrix1D multWithMissing ( DoubleMatrix2D a , DoubleMatrix1D b ) {
@@ -212,8 +211,8 @@ public static DoubleMatrix1D multWithMissing( DoubleMatrix2D a, DoubleMatrix1D b
212211 /**
213212 * Multiple two matrices, tolerate missing values.
214213 *
215- * @param a
216- * @param b
214+ * @param a
215+ * @param b
217216 * @return
218217 */
219218 public static DoubleMatrix2D multWithMissing ( DoubleMatrix2D a , DoubleMatrix2D b ) {
@@ -245,16 +244,16 @@ public static DoubleMatrix2D multWithMissing( DoubleMatrix2D a, DoubleMatrix2D b
245244 }
246245
247246 public static List <Integer > notNearlyZeroIndices ( DoubleMatrix1D d ) {
248- List <Integer > result = new ArrayList <Integer >();
247+ List <Integer > result = new ArrayList <>();
249248 for ( int i = 0 ; i < d .size (); i ++ ) {
250249 if ( Math .abs ( d .getQuick ( i ) ) > Constants .SMALL ) result .add ( i );
251250 }
252251 return result ;
253252 }
254253
255254 /**
256- * @param data
257- * @return a copy of the data with missing values removed (might be empty!)
255+ * @param data
256+ * @return a copy of the data with missing values removed (might be empty!)
258257 */
259258 public static DoubleMatrix1D removeMissing ( DoubleMatrix1D data ) {
260259 int sizeWithoutMissingValues = sizeWithoutMissingValues ( data );
@@ -272,6 +271,31 @@ public static DoubleMatrix1D removeMissing( DoubleMatrix1D data ) {
272271 return r ;
273272 }
274273
274+ /**
275+ * Remove values from data corresponding to missing values in reference.
276+ *
277+ * @param reference
278+ * @param data
279+ * @return
280+ */
281+ public static DoubleMatrix1D removeMissing ( DoubleMatrix1D reference , DoubleMatrix1D data ) {
282+ if ( data .size () != reference .size () ) throw new IllegalArgumentException ( "Reference and data must have same size" );
283+ int sizeWithoutMissingValues = sizeWithoutMissingValues ( reference );
284+ if ( sizeWithoutMissingValues == reference .size () ) return data ; // no missing values.
285+ DoubleMatrix1D r = new DenseDoubleMatrix1D ( sizeWithoutMissingValues );
286+ double [] elements = data .toArray ();
287+ double [] refels = reference .toArray ();
288+ int size = data .size ();
289+ int j = 0 ;
290+ for ( int i = 0 ; i < size ; i ++ ) {
291+ if ( Double .isNaN ( refels [i ] ) || Double .isInfinite ( refels [i ] ) ) {
292+ continue ;
293+ }
294+ r .set ( j ++, elements [i ] );
295+ }
296+ return r ;
297+ }
298+
275299 public static DoubleMatrix1D select ( DoubleMatrix1D v , Collection <Integer > selected ) {
276300 DoubleMatrix1D result = new DenseDoubleMatrix1D ( selected .size () );
277301 int k = 0 ;
@@ -301,8 +325,8 @@ public static DoubleMatrix2D selectColumns( DoubleMatrix2D n, Collection<Integer
301325 }
302326
303327 /**
304- * @param n square matrix
305- * @param selected
328+ * @param n square matrix
329+ * @param selected
306330 * @return
307331 */
308332 public static DoubleMatrix2D selectColumnsAndRows ( DoubleMatrix2D n , Collection <Integer > selected ) {
@@ -371,7 +395,7 @@ public static int sizeWithoutMissingValues( DoubleMatrix1D list ) {
371395 /**
372396 * Makes a copy
373397 *
374- * @param vector
398+ * @param vector
375399 * @return
376400 */
377401 public static DoubleArrayList toList ( DoubleMatrix1D vector ) {
0 commit comments