0

Can any body help to write a diagonal matrix A like this in R, please help in this regard

> A [,1] [,2] [,3] [,4] [,5] [,6] [1,] -1 0 0 0 0 0 [2,] 0 -1 0 0 0 0 [3,] 0 0 -1 0 0 0 [4,] 0 0 0 -1 0 0 [5,] 0 0 0 0 -1 0 [6,] 0 0 0 0 0 -1 [7,] 0 0 0 0 0 1 
0

2 Answers 2

4

The matrix you have isn't exactly diagonal since it isn't square. However, you can see there's a diagonal matrix in there.

The following code reproduces the matrix

 A1 <- diag(-1,6) ## creates a 6x6 diag with -1 on the diag A2 <- c(rep(0,5),1) ## make the bottom row A <- rbind(A1,A2) ## put A1 on top of A2 

Then you can see

 > A [,1] [,2] [,3] [,4] [,5] [,6] -1 0 0 0 0 0 0 -1 0 0 0 0 0 0 -1 0 0 0 0 0 0 -1 0 0 0 0 0 0 -1 0 0 0 0 0 0 -1 A2 0 0 0 0 0 1 
Sign up to request clarification or add additional context in comments.

1 Comment

According to Wikipedia, a diagonal matrix does not necessarily need to be square (Today I Learned). Nevertheless, OPs matrix is indeed not diagonal.
0

You can try the following:

import numpy as np A = np.zeros((7,6)) j = 0 for i in range(6): A[j,i] = -1 j= j+ 1 A[j,5] = 1 print(A) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.