matrix

lectcode-搜索二维矩阵

喜欢而已 提交于 2020-01-21 12:45:57
要求 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性: 每行中的整数从左到右按升序排列。 每行的第一个整数大于前一行的最后一个整数。 示例 示例 1: 输入: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 输出: true 示例 2: 输入: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 13 输出: false 代码 顺序查找,先确定行,再确定列。 bool searchMatrix(vector<vector<int>>& matrix, int target) { int m = matrix.size(); int i = 0; if(m==0) return false; int n = matrix[0].size(); if(n==0) return false; for(;i<m;i++) { if(n==1&&matrix[i][0]==target) return true; if(n>1&&matrix[i][0]<=target&&target<=matrix[i][n-1]) break; } if(i==m) i = i-1

How to create a 2D-matrix out of my data for surf()?

ぐ巨炮叔叔 提交于 2020-01-21 12:31:07
问题 I have a 25000x3-matrix, with each row containing a x-, a y- and a z-value. Now I wanted to do a graphical plot out of these. But for using for example surf(Z) I have to use a mxn-matrix as Z with m equal the size of x and n equal the size of y. How can I reshape the matrix I have to the needed mxn-matrix? The problem is that my x- and y-values are no ints, but floats, so I assume that I have to do a interpolation first. Is that true? My data plotted with plot3 looks like: 回答1: The fact that

How to create a 2D-matrix out of my data for surf()?

核能气质少年 提交于 2020-01-21 12:30:07
问题 I have a 25000x3-matrix, with each row containing a x-, a y- and a z-value. Now I wanted to do a graphical plot out of these. But for using for example surf(Z) I have to use a mxn-matrix as Z with m equal the size of x and n equal the size of y. How can I reshape the matrix I have to the needed mxn-matrix? The problem is that my x- and y-values are no ints, but floats, so I assume that I have to do a interpolation first. Is that true? My data plotted with plot3 looks like: 回答1: The fact that

Need help converting a BMP image to a matrix in [R]?

巧了我就是萌 提交于 2020-01-21 11:43:09
问题 I'm very new to R, and I was wondering if there was a simple way to convert a BMP image to a matrix in R. Mainly, I'm looking for any package that can help. The values of each element in the matrix would correspond to colors. 回答1: A search for "bmp" on the CRAN packages list pulls up bmp and some others, for brevity I will just use this package. library(bmp) fl <- system.file("images", "5HT1bMARCM-F000001_seg001_lsm.bmp", package = "bmp") b <- read.bmp(fl) This object is an array, with some

Conversion euler to matrix and matrix to euler

断了今生、忘了曾经 提交于 2020-01-21 01:47:29
问题 I'm trying to convert a 3D rotation described in term of euler angles into a matrix and then back, using .NET/C#. My conventions are: left handed system (x right, y top, z forward) order of rotations: heading around y, pitch around x, bank around z rotations are positive using the left hand rule (thumb pointing to +infinity) My trial is: Euler to matrix (I've removed the x,y,z translation part for simplification) Matrix3D matrix = new Matrix3D() { M11 = cosH * cosB - sinH * sinP * sinB, M12 =

Haskell linear algebra?

人盡茶涼 提交于 2020-01-20 20:06:12
问题 I am starting to test Haskell for linear algebra. Does anyone have any recommendations for the best package for this purpose? Any other good resources for doing basic matrix manipulation with Haskell? The haskell wiki lists several resources for this. My current focus in on hmatrix and bindings-gsl, both of which look promising. 回答1: The hmatrix and hmatrix-static libraries are excellent. Hunt around on Hackage some more: http://hackage.haskell.org/package/vect 来源: https://stackoverflow.com

Haskell linear algebra?

独自空忆成欢 提交于 2020-01-20 20:05:59
问题 I am starting to test Haskell for linear algebra. Does anyone have any recommendations for the best package for this purpose? Any other good resources for doing basic matrix manipulation with Haskell? The haskell wiki lists several resources for this. My current focus in on hmatrix and bindings-gsl, both of which look promising. 回答1: The hmatrix and hmatrix-static libraries are excellent. Hunt around on Hackage some more: http://hackage.haskell.org/package/vect 来源: https://stackoverflow.com

How to compute only the diagonal of a matrix product in Octave?

我怕爱的太早我们不能终老 提交于 2020-01-20 01:47:05
问题 Is there a way in Octave to compute and store only the diagonal of a matrix product? Basically like doing: vector = diag(A*B); I don't care about any of the values of A*B except those on the diagonal. The matrix sizes are around 80k x 12 and 12 x 80k , so even if I didn't care about the speed/extra memory it simply wont fit in RAM. Strange, since Octave is a package for huge data sets and diagonals are very important, so it should be possible. 回答1: The first element in the diagonal is the

Scale rows of 3D-tensor

ぃ、小莉子 提交于 2020-01-19 18:03:15
问题 I have an n -by- 3 -by- 3 numpy array A and an n -by- 3 numpy array B . I'd now like to multiply every row of every one of the n 3 -by- 3 matrices with the corresponding scalar in B , i.e., import numpy as np A = np.random.rand(10, 3, 3) B = np.random.rand(10, 3) for a, b in zip(A, B): a = (a.T * b).T print(a) Can this be done without the loop as well? 回答1: You can use NumPy broadcasting to let the elementwise multiplication happen in a vectorized manner after extending B to 3D after adding a

Division of sparse matrix

寵の児 提交于 2020-01-19 17:39:16
问题 I have a scipy.sparse matrix with 45671x45671 elements. In this matrix, some rows contain only '0' value. My question is, how to divide each row values by the row sum. Obviously, with for loop it's work, but I look for an efficient method... I already tried : matrix / matrix.sum(1) but I have MemoryError issue. matrix / scs.csc_matrix((matrix.sum(axis=1))) but ValueError: inconsistent shapes Other wacky things... Moreover, I want to skip rows with only '0' values. So, if you have any solution