0

I try to go through the matrix and gradually chain the values according to the sequence. First combine the first two values in the first row with the second value in the second row, then combine the first two values in the second row with the second value in the third row, and so on. If it then passes the last line, it returns to the beginning and takes the first three values from the first line and the third value in the second line. Then the first 3 values in the second row with the third value in the fourth row and continues in the same way until the end of the matrix. This is better seen in the example below.

Size matrix is not fixed

Example:

import numpy as np
np.array([[11,12,13,14],
          [21,22,23,24],
          [31,32,33,34],
          [41,42,43,44],
          [51,52,53,54]])

required output:

[11 21]
[21 31]
[31 41]
[41 51]

[11 12 22]
[21 22 32]
[31 32 42]
[41 42 52]

[11 12 13 23]
[21 22 23 33]
[31 32 33 43]
[41 42 43 53]

can anyone help me?

1
  • Hi, what do you mean by "combining"? Commented Mar 12, 2021 at 21:51

1 Answer 1

2

Try this:

>>> [np.column_stack([m[:-1, :n], m[1:, n-1]]) for n in range(2,5)]

[array([[11, 12, 22],
        [21, 22, 32],
        [31, 32, 42],
        [41, 42, 52]]), 

array([[11, 12, 13, 23],
       [21, 22, 23, 33],
       [31, 32, 33, 43],
       [41, 42, 43, 53]]), 

array([[11, 12, 13, 14, 24],
       [21, 22, 23, 24, 34],
       [31, 32, 33, 34, 44],
       [41, 42, 43, 44, 54]])]

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

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.