diagonal

Extract diagonals from a distance matrix in R

帅比萌擦擦* 提交于 2019-12-03 18:17:49
问题 I would like to know how can I extract the values of the first diagonal from a distance matrix. For example: > mymatrix [,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 6 4 [4,] 8 6 > dist(mymatrix) 1 2 3 2 2.828427 3 5.385165 3.000000 4 8.062258 5.385165 2.828427 I want to get in a vector the values: 2.828427, 3.000000, 2.828427 Thanks! 回答1: One work around is to convert the dist object to matrix and then extract elements where row index is one larger than the column index: mat = as.matrix(dist(mymatrix))

How to create a symmetric matrix where each row/column is a subset of a known vector [duplicate]

☆樱花仙子☆ 提交于 2019-12-03 18:11:30
问题 This question already has an answer here : How do I generate the following matrix and vector from the given input data in MATLAB? (1 answer) Closed 2 years ago . I have a 7*1 vector a = (1:7).' . I want to form a matrix A of size 4*4 from vector a such that the elements of a form the anti-diagonals of matrix A as follows: A = [1 2 3 4; 2 3 4 5; 3 4 5 6; 4 5 6 7] I would like this to work for a general a , not just when the elements are consecutive integers. I appreciate any help. 回答1: You can

Obtaining opposite diagonal of a matrix in Matlab

谁说胖子不能爱 提交于 2019-12-03 16:25:38
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. Here is my matrix, produced by A = magic(5) A = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18

Removing diagonal elements from matrix in R

你说的曾经没有我的故事 提交于 2019-12-03 13:04:44
How can I remove the diagonal elements (diagL) from my matrix L using R? I tried using the following: subset(L, select=-diag(L)) or subset(L, select=-c(diag(L))) but I get 0 numbers... The R programming language? I like C better, it is easier to spell. One way is to create a matrix with the numbers the way I like them to look: a<-t(matrix(1:16,nrow=4,ncol=4)) which looks like: [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 5 6 7 8 [3,] 9 10 11 12 [4,] 13 14 15 16 Delete the values on the diagonal: diag(a)=NA which results in: [,1] [,2] [,3] [,4] [1,] NA 2 3 4 [2,] 5 NA 7 8 [3,] 9 10 NA 12 [4,] 13 14 15

How to create dynamic diagonal line from left-bottom to right-top corner?

╄→гoц情女王★ 提交于 2019-12-03 08:39:57
问题 I've created a simple layout where I have three divs which interact. One is the logo in the middle of the screen and the other are two blocks which with jQuery are moved out of the screen. I used the skew option from CSS to apply a degree transformation. I would like to apply the certain degree depending on the screen, so this degree will apply to all screens correctly. Visual example: http://jsfiddle.net/6a93T/1/ For now I have this code: HTML: <html> <header> <link rel="stylesheet" type=

Create diagonal border of a cell

可紊 提交于 2019-12-03 03:36:18
I wonder if it is even possible creating a table with diagonal border line using css or jquery just like below: Any ideas will be appreciated. Anything is possible if you fiddle around with it long enough, here's an example using some creative borders and a lot of CSS: .arrow_box:after, .arrow_box:before { top: 100%; border: solid transparent; content: " "; height: 0; width: 0; position: absolute; pointer-events: none; } FIDDLE And another one using CSS3 rotate: -webkit-transform: rotate(26.5deg); -moz-transform: rotate(26.5deg); -ms-transform: rotate(26.5deg); -o-transform: rotate(26.5deg);

How to replace non-diagonal elements in a matrix?

天涯浪子 提交于 2019-12-02 21:40:47
问题 More specifically, I want all elements other than the diagonal ones (X_11, X_22, X_33,...,X_jj) to be zero. E.g. I want: [1 4 5 2 3 5 3 9 8] to be: [1 0 0 0 3 0 0 0 8] Is this possible? Sorry I'm a complete noob at this.. 回答1: The simplest way to do this, is to create a new matrix filled with 0s, then replace its diagonal with the diagonal of the old matrix. So if you have: m <- cbind(c(1,2,3), c(4,3,9), c(5, 5, 8)) # The original matrix diagonal <- diag(m) m <- matrix(0, nrow(m), ncol(m)) #

How to create dynamic diagonal line from left-bottom to right-top corner?

眉间皱痕 提交于 2019-12-02 21:12:43
I've created a simple layout where I have three divs which interact. One is the logo in the middle of the screen and the other are two blocks which with jQuery are moved out of the screen. I used the skew option from CSS to apply a degree transformation. I would like to apply the certain degree depending on the screen, so this degree will apply to all screens correctly. Visual example: http://jsfiddle.net/6a93T/1/ For now I have this code: HTML: <html> <header> <link rel="stylesheet" type="text/css" href="css/reset.css"> <link rel="stylesheet" type="text/css" href="css/style.css"> <script type

matlab: filling matrix diagonalwise [duplicate]

ぃ、小莉子 提交于 2019-12-02 10:17:53
This question already has an answer here: adding values to diagonals of matrix using element-wise addition in matlab 3 answers I have an (2n-1)-by-1 vector with certain values and I want to obtain an n-n matrix with the diagonals filled using the same value. Eg. if I have a = [1; 2; 3; 4; 5]; I want to obtain A = [[3 4 5];[2 3 4];[1 2 3]] = 3 4 5 2 3 4 1 2 3 My matrix dimensions are a lot bigger so I'd want this as efficient as possible. I already found following solutions: n = 3; A = toeplitz(a); A = A(1:n,end-n+1:end) and A = a(n)*eye(n); for j=1:n-1 A(1+j:n+1:end-j*n) = a(n-j); A(j*n+1:n+1

How to find the sum of elements above and below the diagonal of a matrix in python?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 06:50:33
问题 I need to find the sum of the elements that are above and below the main diagonal. I have no idea how to condition the algorithm to sum only those numbers. This is the code I have so far, with A being the matrix A = [] N = int(raw_input("Input matrix size: ")) for i in range(0, N): row = [] for j in range(0, N): row.append(int(raw_input("Input elements: "))) A.append(row) sum = 0 for i in range(0, N): sum += A[i][i] print sum sum2 = 0 for i in range(0, N): for j in range(i+1, N): sum2 += A[i]