diagonal

How to replace the the diagonal elements of a matrix by a vector in SymPy?

落花浮王杯 提交于 2019-12-05 20:25:52
I have a vector X which I created like this: from sympy import * x1 = Symbol('x1') x2 = Symbol('x2') x3 = Symbol('x3') X = Matrix([x1, x2, x3]) Then I also have a matrix myMat which just contains ones: myMat = ones(3, 3) Matrix([ [1, 1, 1], [1, 1, 1], [1, 1, 1]]) Now I would like to replace the diagonal of the matrix by my vector X ; my desired outcome looks like this: Matrix([ [x1, 1, 1], [1, x2, 1], [1, 1, x3]]) I can of course do it in a for-loop like this: for ind, el in enumerate(X): myMat[ind, ind] = el but I am wondering whether there is a smarter way of doing that by directly accessing

Diagonal line across view

喜欢而已 提交于 2019-12-05 18:26:54
Based on some condition, I have to diagonally cut the list cell. For this I have made diagonal drawable image using this code: diagonal_line.xml <?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:top="0dp" android:bottom="0dp" > <rotate android:fromDegrees="315" android:toDegrees="315" android:pivotX="0%" android:pivotY="0%" > <shape android:shape="line" > <stroke android:width="10dp" android:color="@color/grey" /> </shape> </rotate> </item> </layer-list> In list cell's xml it is used as: <ImageView android:layout

How to sum over diagonals of data frame

我怕爱的太早我们不能终老 提交于 2019-12-05 16:49:05
问题 Say that I have this data frame: 1 2 3 4 100 8 12 5 14 99 1 6 4 3 98 2 5 4 11 97 5 3 7 2 In this above data frame, the values indicate counts of how many observations take on (100, 1), (99, 1) , etc. In my context, the diagonals have the same meanings: 1 2 3 4 100 A B C D 99 B C D E 98 C D E F 97 D E F G How would I sum across the diagonals (i.e., sum the counts of the like letters) in the first data frame? This would produce: group sum A 8 B 13 C 13 D 28 E 10 F 18 G 2 For example, D is 5+5+4

Lower triangular matrix in julia

夙愿已清 提交于 2019-12-05 16:24:26
I have the number of columns equals the number of rows. And the the diagonal is is equal to zero. How can I build this matrix? #mat # [,1] [,2] [,3] [,4] #[1,] 0 NA NA NA #[2,] 1 0 NA NA #[3,] 2 4 0 NA #[4,] 3 5 6 0 I tried this x=rand(4,4) 4x4 Array{Float64,2}: 0.60064 0.917443 0.561744 0.135717 0.106728 0.72391 0.0894174 0.0656103 0.410262 0.953857 0.844697 0.0375045 0.476771 0.778106 0.469514 0.398846 c=LowerTriangular(x) 4x4 LowerTriangular{Float64,Array{Float64,2}}: 0.60064 0.0 0.0 0.0 0.106728 0.72391 0.0 0.0 0.410262 0.953857 0.844697 0.0 0.476771 0.778106 0.469514 0.398846 but l'm

Multiple constant to a matrix and convert them into block diagonal matrix in matlab

好久不见. 提交于 2019-12-05 16:22:55
I have a1 a2 a3. They are constants. I have a matrix A. What I want to do is to get a1*A, a2*A, a3*A three matrices. Then I want transfer them into a diagonal block matrix. For three constants case, this is easy. I can let b1 = a1*A, b2=a2*A, b3=a3*A, then use blkdiag(b1, b2, b3) in matlab. What if I have n constants, a1 ... an. How could I do this without any looping?I know this can be done by kronecker product but this is very time-consuming and you need do a lot of unnecessary 0 * constant. Thank you. Discussion and code This could be one approach with bsxfun(@plus that facilitates in

Obtaining opposite diagonal of a matrix in Matlab

做~自己de王妃 提交于 2019-12-05 01:24:28
问题 Let A be an matrix of size [n,n] . If I want to extract its diagonal, I do diag(A) . Actually, I want the opposite diagonal, which would be [A(n,1),A(n-1,2),A(n-2,3),...] . One way to do this is via diag(flipud(A)) . However, flipud(A) is quite wasteful and multiplies the time it takes by a factor of 10 compared to finding the usual diagonal. I'm looking for a fast way of obtaining the opposite diagonal. Naturally, for loops seem abysmally slow. Suggestions would be greatly appreciated. 回答1:

rowsum for matrix over specified number of columns in R

旧时模样 提交于 2019-12-04 11:11:41
I'm trying to get the sum of columns in a matrix in R for a certain row. However, I don't want the whole row to be summed but only a specified number of columns i.e. in this case all column above the diagonal. I have tried sum and rowSums function but they are either giving me strange results or an error message. To illustrate, please see example code for an 8x8 matrix below. For the first row I need the sum of the row except item [1,1], for second row the sum except items [2,1] and [2,2] etc. m1 <- matrix(c(0.2834803,0.6398198,0.0766999,0.0000000,0.0000000,0.0000000,0.0000000,0.0000000, 0

CSS: Making two complementary diagonal divs

柔情痞子 提交于 2019-12-04 05:49:32
问题 this is what I'm trying to do: This is how I was trying to do it: The first div ' subCont1 ' works fine, even if it's getting out of the page . If I do the same thing with the second, this time scroll activates for the same reason . .subCont1{ background-color: blueviolet; position:absolute; left:-20%; top:0; width:70%; height:100%; transform: skew(-20deg); border:2px solid yellow; } .subCont2{ width:50%; height:100%; border:2px solid grey; z-index:1000; overflow:hidden; } 回答1: I made this

Check if column or diagonal in matrix = x (Without Numpy)

孤者浪人 提交于 2019-12-03 23:11:38
问题 I can use this code to check if a row in a matrix = x: q = [[1,2,1],[1,2,1],[2,1,2]] answer = [sum(row) for row in q] for i in range(0, len(q)): if answer[i] == 6: print "Player 2 won!" if answer[i] == 3: print "Player 1 won!" if answer[i] != 6 and 3: print "It's a tie!" How can I check if my matrix has a diagonal or column that = x, without using Numpy (Is there a mathematical way to do it as shown above?) Example: (X = something that doesn't matter) q = [[1,X,X],[1,X,X],[1,X,X]] Should

Extended block diagonal matrix in Matlab

限于喜欢 提交于 2019-12-03 21:25:30
问题 I know that to generate a block-diagonal matrix in Matlab the command blkdiag generates such a matrix: Now I am faced with generating the same block-diagonal matrix, but with also matrix elements B_1 , B_2 ,..., B_{n-1} on the upper diagonal, zeros elsewhere: I guess this can be hardcoded with loops, but I would like to find a more elegant solution. Any ideas on how to implement such a thing? P.S. I diag command, that using diag(A,k) returns the k th diagonal. I need something for writing in