## Set values along a diagonal in a matrix

change diagonal of matrix matlab
set diagonal of matrix to 0
diagonal elements of a matrix
change the diagonal of a to 1 without define the matrix again
reverse diagonal matrix matlab
generate a diagonal matrix in matlab
matlab array to diagonal matrix
matlab diagonal matrix of ones

I am trying to use the `matrix()` and `diag()` functions to create the following pattern, but with a 100 x 100 matrix rather than 5 x 5.

5 x 5 matrix:

```| 0 1 0 0 0 |
| 1 0 1 0 0 |
| 0 1 0 1 0 |
| 0 0 1 0 1 |
| 0 0 0 1 0 |
```

In other words, I want to have two diagonals with values of 1, one to the left of the main diagonal, and one to the right of the main diagonal.

The `diag()` function (actually the `diag<-` function) can be used for assignment:

```mat <- matrix( 0, 100,100)
diag(mat) <- 1
mat[1:10,1:10]
#-----------
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    0    0    0    0    0    0    0    0     0
[2,]    0    1    0    0    0    0    0    0    0     0
[3,]    0    0    1    0    0    0    0    0    0     0
[4,]    0    0    0    1    0    0    0    0    0     0
[5,]    0    0    0    0    1    0    0    0    0     0
[6,]    0    0    0    0    0    1    0    0    0     0
[7,]    0    0    0    0    0    0    1    0    0     0
[8,]    0    0    0    0    0    0    0    1    0     0
[9,]    0    0    0    0    0    0    0    0    1     0
[10,]    0    0    0    0    0    0    0    0    0     1
```

You, however, want the sub-diagonal and super-diagonal to be assigned values, so use logical expressions with `col` and `row`:

```mat <- matrix( 0, 100,100)
mat[row(mat)==col(mat)-1] <- 1
mat[row(mat)==col(mat)+1] <- 1
mat[1:10,1:10]
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    0    1    0    0    0    0    0    0    0     0
[2,]    1    0    1    0    0    0    0    0    0     0
[3,]    0    1    0    1    0    0    0    0    0     0
[4,]    0    0    1    0    1    0    0    0    0     0
[5,]    0    0    0    1    0    1    0    0    0     0
[6,]    0    0    0    0    1    0    1    0    0     0
[7,]    0    0    0    0    0    1    0    1    0     0
[8,]    0    0    0    0    0    0    1    0    1     0
[9,]    0    0    0    0    0    0    0    1    0     1
[10,]    0    0    0    0    0    0    0    0    1     0
```

(This method does not depend on having a square matrix. I have a vague memory that there is a faster method that does not require using `row` and `col`. For very large objects each of those functions returns a matrix of the same dimensions as their arguments.)

Is there any way to assign a value to the diagonals of a matrix (not , I have a vector of values that I want to assign to the diagonals of a matrix. to v(2 ), all of the elements of the third (and main) diagonal equal to v(8), and so on. An identity matrix of any size, or any multiple of it (a scalar matrix), is a diagonal matrix. A diagonal matrix is sometimes called a scaling matrix, since matrix multiplication with it results in changing scale (size). Its determinant is the product of its diagonal values.

For the main diagonal, the row and column indices are the same. For the other diagonals, there is a difference of `1` between the row index and column index. Generate those indices directly and assign values in those indices.

```sz = 5
m = matrix(0, sz, sz)
inds1 = cbind(r = 1:(sz-1), c = 2:sz)
inds2 = cbind(r = 2:sz, c = 1:(sz-1))
m[inds1] = 1
m[inds2] = 1
m

# OR, to make it concise
m = matrix(0, sz, sz)
inds = rbind(cbind(1:(sz-1), 2:sz), cbind(2:sz, 1:(sz-1)))
replace(m, inds, 1)

#     [,1] [,2] [,3] [,4] [,5]
#[1,]    0    1    0    0    0
#[2,]    1    0    1    0    0
#[3,]    0    1    0    1    0
#[4,]    0    0    1    0    1
#[5,]    0    0    0    1    0
```

How to assign values on the diagonal?, How to assign values on the diagonal? matlab vectorization. Suppose I have an NxN matrix A, an index vector V consisting of a subset of the numbers 1:  Set values along a diagonal in a matrix. Ask Question Asked 1 year, 3 months ago. Active 1 year, 3 months ago. Viewed 73 times 3. I am trying to

We could create a function using a math trick which would work for all square matrix.

```get_off_diagonal_1s <- function(n) {
#Create a matrix with all 0's
mat <- matrix(0, ncol = n, nrow = n)
#Subtract row indices by column indices
inds = row(mat) - col(mat)
#Replace values where inds is 1 or -1
mat[inds == 1 | inds == -1] = 1
mat
}

get_off_diagonal_1s(5)
#     [,1] [,2] [,3] [,4] [,5]
#[1,]    0    1    0    0    0
#[2,]    1    0    1    0    0
#[3,]    0    1    0    1    0
#[4,]    0    0    1    0    1
#[5,]    0    0    0    1    0

get_off_diagonal_1s(8)
#     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
#[1,]    0    1    0    0    0    0    0    0
#[2,]    1    0    1    0    0    0    0    0
#[3,]    0    1    0    1    0    0    0    0
#[4,]    0    0    1    0    1    0    0    0
#[5,]    0    0    0    1    0    1    0    0
#[6,]    0    0    0    0    1    0    1    0
#[7,]    0    0    0    0    0    1    0    1
#[8,]    0    0    0    0    0    0    1    0
```

Program to convert the diagonal elements of the matrix to 0 , of Pair in C++ STL with Examples · Priority Queue of Vectors in C++ STL with Examples · Set of Vectors in C++ STL with Examples The task is to convert the elements of diagonal of a matrix to 0. Recommended: Please try your approach on {IDE} first, before moving on to the solution. C++ program to change value of . D = diag (v) returns a square diagonal matrix with the elements of vector v on the main diagonal. D = diag (v,k) places the elements of vector v on the k th diagonal. k=0 represents the main diagonal, k>0 is above the main diagonal, and k<0 is below the main diagonal.

numpy.fill_diagonal, Value to be written on the diagonal, its type must be compatible with that of the array a. wrapbool. For tall matrices in NumPy version up to 1.6.2, the diagonal  If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of the form a [i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-array whose diagonal is returned.

numpy.diagonal, If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of the returned a new, independent array containing a copy of the values in the diagonal. In NumPy 1.9 it returns a read-only view on the original array. numpy.fill_diagonal¶ numpy.fill_diagonal (a, val, wrap=False) [source] ¶ Fill the main diagonal of the given array of any dimensionality. For an array a with a.ndim >= 2, the diagonal is the list of locations with indices a[i,, i] all identical. This function modifies the input array in-place, it does not return a value.

Diagonal matrix, In linear algebra, a diagonal matrix is a matrix in which the entries outside the main diagonal Its determinant is the product of its diagonal values. that is, a scalar multiple λI of the identity matrix I. Its effect on a vector is scalar multiplication by λ. with diagonal matrices (its centralizer is the set of diagonal matrices). If a is 2-D, returns the diagonal of a with the given offset, i.e., the collection of elements of the form a[i, i+offset]. If a has more than two dimensions, then the axes specified by axis1 and axis2 are used to determine the 2-D sub-array whose diagonal is returned.