Home > Net >  How can i build matrices in numpy
How can i build matrices in numpy

Time:08-13

I want to build a matrix in NumPy in which the items add up to each other. So I have tried to build it with the following code:


StartpointRow = int(input("First number of row?:\n"))
EndpointRow = int(input("Last number of row?:\n"))
StepRow = int(input("Which steps should the row have?:\n"))

StartpointCol = int(input("First number of column?:\n"))
EndpointCol = int(input("Last number of column?:\n"))
StepCol = int(input("Which steps should the column have?:\n"))


x = np.array([[i j for i in range(StartpointCol, EndpointCol , StepCol)]
              for j in range(StartpointRow, EndpointRow , StepRow)])
print(x)

let's say that, for instance, I enter 1,4,1 and 1,4,1. I want the solution to be a matrix like this:

1 2 3 4
2 4 5 6
3 5 6 7 
4 6 7 8

Not like that:

 2 3 4
 3 4 5
 4 5 6

or If the user types in: 1,4,1 and 2,4,1.

0 1 2 3 4
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8

not like that:

  3 4
  4 5
  5 6

Could you help me out?

CodePudding user response:

Use np.add.outer:

def build(r_start, r_stop, r_step, c_start, c_stop, c_step):
    r = np.arange(r_start, r_stop   1, r_step)
    c = np.arange(c_start, c_stop   1, c_step)
    if r_start == c_start:
        ret = np.empty((c.size, r.size), int)
        ret[:, 0] = c
        ret[0, :] = r
    else:
        ret = np.empty((c.size   1, r.size   1), int)
        ret[0, 0] = 0
        ret[1:, 0] = c
        ret[0, 1:] = r

    np.add.outer(ret[1:, 0], ret[0, 1:], out=ret[1:, 1:])
    return ret

A little simplification:

def build(r_start, r_stop, r_step, c_start, c_stop, c_step):
    r = np.arange(r_start, r_stop   1, r_step)
    c = np.arange(c_start, c_stop   1, c_step)
    ne = int(r_start != c_start)
    ret = np.empty((c.size   ne, r.size   ne), int)
    ret[0, 0] = 0
    ret[ne:, 0] = c
    ret[0, ne:] = r
    np.add.outer(ret[1:, 0], ret[0, 1:], out=ret[1:, 1:])
    return ret

Test:

>>> build(1, 4, 1, 1, 4, 1)
array([[1, 2, 3, 4],
       [2, 4, 5, 6],
       [3, 5, 6, 7],
       [4, 6, 7, 8]])
>>> build(1, 4, 1, 2, 4, 1)
array([[0, 1, 2, 3, 4],
       [2, 3, 4, 5, 6],
       [3, 4, 5, 6, 7],
       [4, 5, 6, 7, 8]])

CodePudding user response:

I think your test cases are wrong. What I understand you mean is that each row and each column have a starting number, what needs to be done is to add the two and generate the matrix according to step. If the user types in: 1,4,1 and 1,4,1, what he can get is:

 2 3 4 5
 3 4 5 6
 4 5 6 7
 5 6 7 8

And if the user types in: 1,4,1 and 2,4,1, what he can get is:

 3 4 5
 4 5 6
 5 6 7
 6 7 8

And my code is:

import numpy as np

StartpointRow = int(input("First number of row?:\n"))
EndpointRow = int(input("Last number of row?:\n"))
StepRow = int(input("Which steps should the row have?:\n"))

StartpointCol = int(input("First number of column?:\n"))
EndpointCol = int(input("Last number of column?:\n"))
StepCol = int(input("Which steps should the column have?:\n"))


x = np.array([[i j for i in range(StartpointCol, EndpointCol   1 , StepCol)]
              for j in range(StartpointRow, EndpointRow   1, StepRow)])
print(x)
  • Related