How to subset matrix to one column, maintain matrix data type, maintain row/column names?
Use the drop=FALSE argument to [. m <- matrix(1:10,5,2) rownames(m) <- 1:5 colnames(m) <- 1:2 m[,1] # vector m[,1,drop=FALSE] # matrix
Use the drop=FALSE argument to [. m <- matrix(1:10,5,2) rownames(m) <- 1:5 colnames(m) <- 1:2 m[,1] # vector m[,1,drop=FALSE] # matrix
you can use rowSums rowSums(data) should give you what you want.
Use Overloaded Arrays#Sort(T[] a, Comparator c) which takes Comparator as the second argument. double[][] array= { {1, 5}, {13, 1.55}, {12, 100.6}, {12.1, .85} }; java.util.Arrays.sort(array, new java.util.Comparator<double[]>() { public int compare(double[] a, double[] b) { return Double.compare(a[0], b[0]); } }); JAVA-8: Instead of that big comparator, we can use lambda function as following- Arrays.sort(array, … Read more
You can just as easily access each element in the list using e.g. path[[1]]. You can’t put a set of matrices into an atomic vector and access each element. A matrix is an atomic vector with dimension attributes. I would use the list structure returned by split, it’s what it was designed for. Each list … Read more
Try: mmatrix = np.zeros((nrows, ncols)) Since the shape parameter has to be an int or sequence of ints http://docs.scipy.org/doc/numpy/reference/generated/numpy.zeros.html Otherwise you are passing ncols to np.zeros as the dtype.
Many built-in operations like sum and prod are already able to operate across rows or columns, so you may be able to refactor the function you are applying to take advantage of this. If that’s not a viable option, one way to do it is to collect the rows or columns into cells using mat2cell … Read more
One option is to use do.call(): > do.call(rbind, a) [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 1 2 3 4 5 [2,] 2 1 2 3 4 5 [3,] 3 1 2 3 4 5 [4,] 4 1 2 3 4 5 [5,] 5 1 2 3 4 5 [6,] 6 1 2 3 … Read more
np.count_nonzero(~np.isnan(data)) ~ inverts the boolean matrix returned from np.isnan. np.count_nonzero counts values that is not 0\false. .sum should give the same result. But maybe more clearly to use count_nonzero Testing speed: In [23]: data = np.random.random((10000,10000)) In [24]: data[[np.random.random_integers(0,10000, 100)],:][:, [np.random.random_integers(0,99, 100)]] = np.nan In [25]: %timeit data.size – np.count_nonzero(np.isnan(data)) 1 loops, best of 3: … Read more
Gimbal lock is one reason, although as you say it is only a problem with Euler angles and is easily solvable. Euler angles are still used when memory is a concern as you only need to store 3 numbers. For quaternions versus a 3×3 rotation matrix, the quaternion has the advantage in size (4 scalars … Read more
See ?which.max > which.max( matrix[,2] ) [1] 2