2

I am trying to construct a matrix object out of an array. The array has a length of 25, and what I'm trying to do is construct a 5x5 matrix out of it. I have used both numpy.asmatrix() and the matrix constructor but both result in a matrix that has a length of 1. So, what's basically happening is all the elements of the array are considered a tuple and inserted into the newly-created matrix. Is there any way around this so I can accomplish what I want?

EDIT: When I wrote "array", I naively meant a vanilla python list and not an actual numpy.array which would make things a lot simpler. A mistake on my part.

AutomEng
  • 425
  • 2
  • 9
  • 19
  • Note the [difference between array and matrix in numpy](http://stackoverflow.com/q/4151128/1461850). – Lee Nov 09 '15 at 15:36

4 Answers4

5

Think you probably just want .reshape():

In [2]: a = np.arange(25)

In [3]: a
Out[3]:
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24])

In [4]: a.reshape(5,5)
Out[4]:
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

You can also convert it into an np.matrix after if you need things from that:

In [5]: np.matrix(a.reshape(5,5))
Out[5]:
matrix([[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24]])

EDIT: If you've got a list to start, it's still not too bad:

In [16]: l = range(25)

In [17]: np.matrix(np.reshape(l, (5,5)))
Out[17]:
matrix([[ 0,  1,  2,  3,  4],
        [ 5,  6,  7,  8,  9],
        [10, 11, 12, 13, 14],
        [15, 16, 17, 18, 19],
        [20, 21, 22, 23, 24]])
Randy
  • 14,349
  • 2
  • 36
  • 42
  • I tried reshape() and got the error below which really confuses me: ValueError: total size of new array must be unchanged – AutomEng Nov 09 '15 at 15:32
  • That means that for your call to `arr.reshape(x, y)`, `x * y` is not equal to `len(arr)`. – Randy Nov 09 '15 at 15:35
  • Nevermind, a mistake on my part. The problem is that I'm trying to create a matrix out of a list (instead of an actual numpy array). I'll edit the post. Your answer is fine. Cheers. – AutomEng Nov 09 '15 at 15:38
  • @AutomEng: If his answer is fine, you should accept the answer – dawg Nov 09 '15 at 18:11
1

You can simply simulate a Matrix by using a 2-dimensional array with 5 spaces in each direction:

>>>Matrix =  [[0 for x in range(5)] for x in range(5)]

And access the elemets via:

>>>Matrix[0][0]=1

To test the output, print it:

>>>Matrix
[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

If you need a specific implementation like numpy, please specify your question.

Fr3ak1n0ut
  • 71
  • 9
1
row = int(input("Enter the number of Rows: \n"))
col = int(input("Enter the number of Column: \n"))
print("Enter how many elements you want: \n")
num1 = row * col
print('Enter your elements in array: ')
for i in range(num1):
    n = int(input("Element " + str(i + 1) + " : "))
    num_array1.append(n)
arr = np.array([num_array1])
newarr = arr.reshape(row, col)
print(newarr)
print(type(newarr))

This should help to create matrix type arrays with custom user input

0

If you have an array of length 25, you can turn it into a 5x5 array using reshape().

A = np.arange(25)  # length 25
B = A.reshape(5, 5)  # 5x5 array

You will however have to make sure that the elements in your array end up in the correct place in the newly formed 5x5 array.

Although there is a numpy.matrix class, I would suggest you forget about it and only use numpy.ndarray. The only difference is you have to use np.dot (or @ in case of newer Python/Numpy) for matrix multiplication instead of *. The matrix class have a tendency to introduce mistakes in your code unless you are very careful.

Hannes Ovrén
  • 21,229
  • 9
  • 65
  • 75