Create NxN Matrix using Numpy in Python.

 



Create NxN Matrix in Python/Numpy

One thing that may be inseparable when we do programming is a matrix. For simple applications our data may only consist of 1 row or 1 column, so we don’t consider it as a matrix. However, when we need to handle so much data we need to handle those data in MxN or NxN matrix. We can handle it in the traditional way using python. Usually, people will create it as a list inside a list. Or the fastest way is using Numpy from Scipy library. Using Numpy is advised especially when you need to display the result in matrix form. On the other side, if your data is very large, Numpy will only display it as the first 3 data and the last 3 data.

First, we need to consider row and column as it: (image resource)

xy-rowcol

The row is all data in a horizontal line when the column is in a vertical line. So the first row (row=0) consist of [1,2,3], the middle row (row=1) are [4,5,6] and the last row (row=2) are [7,8,9]. Note that numbering in programming started with 0 even if it’s the first row. Then for each column, you will have [1,4,7][2,5,8], and [3,6,9].

Matrix using python list:

Creating a square matrix will be easier to understand in the beginning. Let say you want to create an NxN matrix which index i=3 (have 3 number of row and 3 number of column):

matrix=[] #define empty matrix
for i in xrange(3): #total row is 3
    row=[] 
    for j in xrange(3): #total column is 3
        row.append(0) #adding 0 value for each column for this row
    matrix.append(row) #add fully defined column into the row
print matrix

You can see the detail in each line. This code will result:

[[0,0,0],[0,0,0],[0,0,0]]

With all the data zero. You can modify it as you wish, such as changing 0 to i then it becomes

[[1,1,1],[2,2,2],[3,3,3]]

or change 0 to j so it will be:

[[1,2,3],[1,2,3],[1,2,3]]

However, if you want the result to be written inside a file, converting each row and column will be tricky.

Then how to make it MxN matrix? You simply make i and j value different:

for i in xrange(3): 
    for j in xrange(2): 

and this will give you:

[[0,0],[0,0],[0,0]]

Matrix using Numpy:

Numpy already has built-in array. It’s not a too different approach for writing the matrix but seems convenient. If you want to create a zero matrix with a total i-number of rows and column just write:

import numpy
i = 3
a = numpy.zeros(shape=(i,i))

And if you want to change the respective data, for example:

for i in xrange(3):
    a[0][i] = i+1
    a[i][0] = i+1

it will give you:

[[1    2    3]
 [2    0    0]
 [3    0    0]]

The result displayed in array format, so it easy for you to observe the matrix. However, in case your data is so large (i.e. 100):

[[  1    2    3 ..., 98   99  100]
 [  2    0    0 ...,  0    0    0]
 [  3    0    0 ...,  0    0    0]
 ...,
 [ 98    0    0 ...,  0    0    0]
 [ 99    0    0 ...,  0    0    0]
 [100    0    0 ...,  0    0    0]]

A similar way if you wish to create MxN matrix using Numpy. For example:

import numpy
i = 3
j = 2
a = numpy.zeros(shape=(i,j))

The good part with Numpy is you can directly save the result in CSV format (or other formats):

numpy.savetxt("filename.csv", a, delimiter=",")

Thanks for going through it!

Comments

Popular Posts