matrix

Checking if a matrix is symmetric in Numpy

冷暖自知 提交于 2020-01-10 17:43:47
问题 I'm trying to make a function with the arguments (a,tol=1e-8) that returns a boolean value that tells the user whether or not the matrix is symmetric (symmetric matrix is equal to its transpose). So far I have: def check_symmetric(a, tol=1e-8): if np.transpose(a, axes=axes) == np.transpose(a, axes=axes): return True def sqr(s): rows = len(s) for row in sq: if len(row) != rows: return False return True if a != sqr(s): raise ValueError although I keep getting an axes isn't defined message so I

A^k for matrix multiplication in R?

不问归期 提交于 2020-01-10 13:54:28
问题 Suppose A is some square matrix. How can I easily exponentiate this matrix in R? I tried two ways already: Trial 1 with a for-loop hack and Trial 2 a bit more elegantly but it is still a far cry from A k simplicity. Trial 1 set.seed(10) t(matrix(rnorm(16),ncol=4,nrow=4)) -> a for(i in 1:2){a <- a %*% a} Trial 2 a <- t(matrix(c(0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0),nrow=4)) i <- diag(4) (function(n) {if (n<=1) a else (i+a) %*% Recall(n-1)})(10) 回答1: Although Reduce is more elegant, a for-loop

Extract the first TRUE per row in logical matrix efficiently in R [duplicate]

元气小坏坏 提交于 2020-01-10 05:08:20
问题 This question already has answers here : How to vectorize this operation on every row of a matrix (3 answers) Closed 3 years ago . Given the following matrix: A B C [1,] TRUE FALSE TRUE [2,] FALSE TRUE TRUE [3,] FALSE FALSE TRUE [4,] FALSE TRUE TRUE [5,] FALSE TRUE TRUE [6,] TRUE TRUE TRUE m <- structure(c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE), .Dim = c(6L, 3L), .Dimnames = list(NULL, c("A", "B", "C"))) How we can

Python advanced slicing

廉价感情. 提交于 2020-01-10 04:34:08
问题 I am a little confused with Python's advanced slicing. I basically had a dictionary and with help from SO, I made it into an array. array1 = ([[[36, 16], [48, 24], [12, 4], [12, 4]], [[48, 24], [64, 36], [16, 6], [16, 6]], [[12, 4], [16, 6], [ 4, 1], [ 4, 1]], [[12, 4], [16, 6], [ 4, 1], [ 4, 1]]]) To practice using matrix solver, the array was turned into a square matrix (4 x 4) using: array_matrix_sized = array[:, :, 0] I read that this means [number of indices, rows, columns]. I am a

Flood Fill in Python

China☆狼群 提交于 2020-01-10 04:26:45
问题 I'm complitely new to Flood Fill algorithm. I checked it out from Wikipedia (http://en.wikipedia.org/wiki/Flood_fill). But didn't become that much wiser. I'm trying to use it in following situation. I have a matrix: matrix = [["a", "a", "b", "a", "a", "b"], ["a", "b", "b", "a", "b", "b"], ["b", "a", "b", "a", "a", "b"], ["b", "a", "b", "a", "b", "b"], ["a", "a", "b", "a", "a", "a"], ["a", "b", "b", "a", "a", "b"]] Then I let user to decide one point from matrix. If in that given point is "b"

Removing duplicate rows from data frame in R

本秂侑毒 提交于 2020-01-10 04:26:07
问题 I have two columns, would like to retain only the non commutative rows.For the data below my output should contain one combination of (1 2). i.e. for my query (1 2) is same as (2 1). Is there a simple way to do it in R. Already tried transposing. and retaining the upper traingular matrix. but it becomes a pain re transposing back the data. A B prob 1 2 0.1 1 3 0.2 1 4 0.3 2 1 0.3 2 3 0.1 2 4 0.4 My final output should be: A B prob 1 2 0.1 1 3 0.2 1 4 0.3 2 3 0.1 2 4 0.4 回答1: We can use data

How to determine if a vector is between two other vectors?

隐身守侯 提交于 2020-01-10 01:22:26
问题 I am looking for a fast and effective way to determine if vector B is Between the small angle of vector A and vector C. Normally I would use the perpendicular dot product to determine which sides of each line B lies on but in this case is not so simple because of the following: None of the vectors can be assumed to be normalized and normalizing them is an extra step I would prefer to avoid. I have no clear notion as to which side is the smallest angle so it is hard to say which side of the

3d to 2d Projection Matrix

天大地大妈咪最大 提交于 2020-01-09 13:58:47
问题 I have 3 points in a 3D space of which I know the exact locations. Suppose they are: (x0,y0,z0) , (x1,y1,z1) and (x2,y2,z2) . Also I have a camera that is looking at these 3 points and I know the 2D locations of those three points on camera view plane. So for example (x0,y0,z0) will be (x0',y0') , and (x1,y1,z1) will be (x1',y1') and (x2,y2,z2) will be (x2',y2') from the camera's point of view. What is the easiest way to find the projection matrix that will project those 3D points into 2D

3d to 2d Projection Matrix

五迷三道 提交于 2020-01-09 13:58:44
问题 I have 3 points in a 3D space of which I know the exact locations. Suppose they are: (x0,y0,z0) , (x1,y1,z1) and (x2,y2,z2) . Also I have a camera that is looking at these 3 points and I know the 2D locations of those three points on camera view plane. So for example (x0,y0,z0) will be (x0',y0') , and (x1,y1,z1) will be (x1',y1') and (x2,y2,z2) will be (x2',y2') from the camera's point of view. What is the easiest way to find the projection matrix that will project those 3D points into 2D

3d to 2d Projection Matrix

二次信任 提交于 2020-01-09 13:58:32
问题 I have 3 points in a 3D space of which I know the exact locations. Suppose they are: (x0,y0,z0) , (x1,y1,z1) and (x2,y2,z2) . Also I have a camera that is looking at these 3 points and I know the 2D locations of those three points on camera view plane. So for example (x0,y0,z0) will be (x0',y0') , and (x1,y1,z1) will be (x1',y1') and (x2,y2,z2) will be (x2',y2') from the camera's point of view. What is the easiest way to find the projection matrix that will project those 3D points into 2D