1

As in the title, if I have a matrix a

a = np.diag(np.arange(5))
array([[0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [0, 0, 2, 0, 0],
       [0, 0, 0, 3, 0],
       [0, 0, 0, 0, 4]])

How can I assign a new 4x4 matrix or even 3x4 matrix to a without i-th row and i-th column? Let's say

b = array([[1,1,1,1],
       [1,1,1,1],
       [1,1,1,1])

I want to slice a and remove the first and second row and the second column of the matrix, which is something in R like

a[c(-1,-2), -2] = b
a = 
array([[0, 0, 0, 0, 0],
       [0, 1, 0, 0, 0],
       [1, 0, 1, 1, 1],
       [1, 0, 1, 1, 1],
       [1, 0, 1, 1, 1]])

But in python, I tried something like

a[[2,3,4],:][:,[0,1,3,4]]
output:
array([0, 2, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 4]])

This operation won't allow me to assign a new matrix to slices of a. How can I do that? I really appreciate any help you can provide.

p.s.

I found in this special case, I can assign values by blocks. But what I actually want to ask is when we do slice like a[2:5, [0,2,3,4]], we can get a 3x4 matrix, and assign a new matrix to that position of the matrix. But I want to do is to slice 'a[[0,2,3,4],[0,2,3,4]]` to get a 4x4 matrix or other shapes(the index for row and column may even be random), and assign a new matrix to that position. But numpy gives me a 1d array.

3 Answers3

1
newmatrix = a[[0, 1, 3, 4], :][:, [0, 1, 3, 4]]
Benjamin Breton
  • 1,388
  • 1
  • 13
  • 42
Pete
  • 11
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 23 '22 at 14:58
  • Thanks for answering, but my question is assigning a new matrix to that slices of a. – Archibald Chain Jun 06 '22 at 09:58
0

Regarding setting the values of a matric part of a larger matrix, I think there is no direct option. But you can create the original matrix around the one to be added:

before = np.array([[0, 0, 0, 0, 0],
                   [0, 1, 0, 0, 0],
                   [0, 0, 2, 0, 0],
                   [0, 0, 0, 3, 0],
                   [0, 0, 0, 0, 4]])

insert_array = np.array([[1, 1, 1, 1],
                         [1, 1, 1, 1],
                         [1, 1, 1, 1]])

first two rows without second column

first_step = np.delete(before[:2, :], 1, 1) or first_step = before[:2, [0, 2, 3, 4]]

appended to insert matrix

second_step = np.insert(insert_array, 0, first_step, axis=0)

second column appended

third_step = np.insert(second_step, 1, before[:, 1], axis=1)

final matrix

third_step = np.array([[0, 0, 0, 0, 0],
                       [0, 1, 0, 0, 0],
                       [1, 0, 1, 1, 1],
                       [1, 0, 1, 1, 1],
                       [1, 0, 1, 1, 1]])
Thomas
  • 507
  • 5
  • 15
  • Thanks for answering. But my question is to assign a new 4x4 matrix to matrix a without i-th row and column – Archibald Chain Jun 06 '22 at 10:00
  • Ah, I see. You can insert and fill your new matrix with values of the old matrix until it has the correct size. I'll check it. – Thomas Jun 07 '22 at 09:36
  • Thank you for answering. I found I can assign values by blocks. But what I actually want to ask is when we do slice like `a[2:5, [0,2,3,4]]`, we can get a 3x4 matrix, and assign a new matrix to that position of the matrix. But I want to do is to slice 'a[[0,2,3,4],[0,2,3,4]]` to get a 4x4 matrix or other shapes(the index for row and column may even be random), and assign a new matrix to that position. But numpy gives me a 1d array. Do you think that even possible? – Archibald Chain Jun 09 '22 at 09:09
0

I can't find a one-step solution to do that. But I think we can assign matrix by block.

a[2:5, 0] = 1
a[2:5, 2:5] = 1

Then I can get what I want.

  • Although this method additionally only works if all elements are set to one value, or can the `1`be replaced by a flattened matrix that fills the original matrix? – Thomas Jun 09 '22 at 09:47
  • Yes, it even works for arrays with: `a[2:5, 0] = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(2, 5)` – Thomas Jun 09 '22 at 10:49
  • Yes, when we have consequent columns or rows to choose, we can work that way: `a[1:5,[1,3,4]] = new4by3matrix`. But if we don't have consequent rows and columns, for example, if I want to choose rows 1,2,4 and columns 1,2,4, I cannot use `a[[1,2,4], [1,2,4]] = new3by3matrix`. – Archibald Chain Jun 09 '22 at 21:50