I am new to Python 3.4 and I usually use MATLAB/ GNU Octave for matrix calculation. I know we can perform matrix calculation using numpy in Python 2.x, but numpy does not work for Python 3.4.
I tried to create this code to input an m by n matrix. I intended to input [[1,2,3],[4,5,6]]
but the code yields [[4,5,6],[4,5,6]
. Same things happen when I input other m by n matrix, the code yields an m by n matrix whose rows are identical.
Perhaps you can help me to find what is wrong with my code.
m = int(input('number of rows, m = '))
n = int(input('number of columns, n = '))
matrix = []; columns = []
# initialize the number of rows
for i in range(0,m):
matrix += [0]
# initialize the number of columns
for j in range (0,n):
columns += [0]
# initialize the matrix
for i in range (0,m):
matrix[i] = columns
for i in range (0,m):
for j in range (0,n):
print ('entry in row: ',i+1,' column: ',j+1)
matrix[i][j] = int(input())
print (matrix)
This code takes number of row and column from user then takes elements and displays as a matrix.
input
output [[1, 2, 3], [4, 5, 6]]
Output:
Note: this code in case of control.it only control no. Of rows but we can enter any number of column we want i.e
row[0]=2
so be careful. This is not the code where you can control no of columns.If your matrix is given in row manner like below, where size is s*s here s=5
5 31 100 65 12 18 10 13 47 157 6 100 113 174 11 33 88 124 41 20 140 99 32 111 41 20
then you can use this
your matrix will be 'arr'