0

For example, I have a matrix:

[ [1 2 3 4 5],

  [6 7 8 9 10],

  [11 12 13 14 15],

  [16 17 18 19 20],

  [21 22 23 24 25] ]

I want to insert [ [-1 -1 -1], [0 5 0] ] in some position, like:

[ [1 2 3 4 5],

  [6 7 8 9 10],

  [11 -1 -1 -1 15],

  [16 0 5 0 20],

  [21 22 23 24 25] ]
4
  • 1
    That's not inserting, that's changing existing values. What have you tried? Commented Dec 10, 2017 at 13:35
  • 1
    It's not unclear how would you define "some position". Reading up on indexing/slicing should help. Commented Dec 10, 2017 at 13:40
  • I have a matrix with pixels of the image (frame t-1). In the next frame, I found blocks that correspond to the offset (motion). Now I'm trying to replace the pixels with the values of these blocks in order to restore the frame. I tried: k = -1 l = -1 for i in range(to_x, to_x+4): k = k + 1 for j in range(to_y, to_y+4): l = l + 1 t_rec[i][j]=im_block[l][k] Commented Dec 10, 2017 at 13:44
  • That last comment probably should be edited into the question itself. Also, the word "replace" (in the comment) seems to be the word you want instead of "insert". Commented Dec 10, 2017 at 14:12

3 Answers 3

2

Use numpy insert! Here is an example from the numpy reference at scipy:

>>> a = np.array([[1, 1], [2, 2], [3, 3]])
>>> a
array([[1, 1],
       [2, 2],
       [3, 3]])
>>> np.insert(a, 1, 5)
array([1, 5, 1, 2, 2, 3, 3])
>>> np.insert(a, 1, 5, axis=1)
array([[1, 5, 1],
       [2, 5, 2],
       [3, 5, 3]]

Read more here: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.insert.html

Sign up to request clarification or add additional context in comments.

Comments

1

Based on the example, I would say you are trying to replace or modify part of the existing array rather than insert an array.

You could use basic slicing to get a view of the part of the array you want to overwrite, and assign the value of that slice to a new matrix of the same size as the slice. For example:

>>> x=np.matrix([[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]])
>>> x
matrix([[ 1,  2,  3,  4],
        [ 5,  6,  7,  8],
        [ 9, 10, 11, 12],
        [13, 14, 15, 16]])
>>> x[1:3,1:4]=np.matrix([[-1,-2,-3],[-4,-5,-6]])
>>> x
matrix([[ 1,  2,  3,  4],
        [ 5, -1, -2, -3],
        [ 9, -4, -5, -6],
        [13, 14, 15, 16]])

In general, to describe a submatrix of m rows and n columns with its upper left corner at row r and column c of the original matrix, index the slice as x[r:r+m,c:c+n].

Comments

0

This method take the matrix m, and replace the elements with array n starting from row r, column c

def replace(m, n, r, c):
    i = 0
    if len(n) + c > len(m[r]):
        return
    for each in n:
        m[r][c] = n[i]
        c += 1
        i += 1

you have to check the index boundaries for the matrix

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.