How to create a matrix?

How to create a Matrix through an iteration in Python

  • I want to create a (3n*7) Matrix in python through Iteration, I did this in VB.Net and it worked but it gives me some challenge in Python as this a new language to me. n can be the number of Iterations to build up the matrix, i.e. ; if u iterated 3 times, the matrix will be a 9*7 matrix. Here is how I did it in VB.Net. : 'Assign some values to the A matrix A_mat(p, 0) = 1 : A_mat(1 + p, 1) = 1 : A_mat(2 + p, 2) = 1 'row/column 1,2 & 3 data A_mat(p, 3) = c : A_mat(p, 4) = 0 : A_mat(p, 5) = a : A_mat(p, 6) = b 'row 1 data A_mat(1 + p, 3) = g : A_mat(1 + p, 4) = d : A_mat(1 + p, 5) = t : A_mat(1 + p, 6) = f 'row 2 data A_mat(2 + p, 3) = m : A_mat(2 + p, 4) = h : A_mat(2 + p, 5) = u : A_mat(2 + p, 6) = k 'row 3 data this yielded: 1 0 0 c 0 a b 0 1 0 g d t f 0 0 1 m h u k . . . . . .

  • Answer:

    One solution is to use a numpy (http://new.scipy.org/download.html) matrix: >>> from numpy import zeros, matrix >>> n = 2 >>> mat = matrix(zeros([3*n, 7])) >>> mat matrix([[ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.]]) However, as noted above by @joris, with a numpy matrix all of your data must be of the same type. >>> mat[0,2] = 14 >>> mat matrix([[ 0., 0., 14., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.], [ 0., 0., 0., 0., 0., 0., 0.]]) >>> mat[0,1] = 's' Traceback (most recent call last): File "<pyshell#139>", line 1, in <module> mat[0,1] = 's' ValueError: could not convert string to float: s You probably want to use a nested list instead, since this is by far a more flexible and generic data type, and in addition is easier to get the hang of. For that I would refer you to http://stackoverflow.com/questions/2397141/how-to-initialize-a-two-dimensional-array-in-python, since @Mike Graham does a right proper job of explaining them. You might want to define a method like: def shape_zeros(n): # Here's where you build up your "matrix"-style nested list pass # The pass statement does nothing! Here are a couple of hints for filling up the code body of the above function. First, you can construct a whole list of duplicates with the * asterisk operator: >>> row = [0] * 7 >>> row [0, 0, 0, 0, 0, 0, 0] Next, you can nest lists within lists; but be careful when moving lists around, as the list's name is actually like a pointer: >>> mat = [] >>> n = 2 >>> for i in range(n*3): mat.append(row) >>> mat [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] >>> mat[0][0] = 1 >>> mat [[1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0]] You can avoid the above problem by creating your sub-lists (rows) separately or using a list(old_list) constructor function to make a copy. When you build it right, you can access/manipulate an element of a nested list like this: >>> mat[0][0] = 1 >>> mat[1][2] = 'Q_Q' >>> mat[2][0] = 3 >>> mat[2][2] = 5 >>> mat [[1, 0, 0, 0, 0, 0, 0], [0, 0, 'Q_Q', 0, 0, 0, 0], [3, 0, 5, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]] Good luck!

Blessed at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

In [13]: [[0]*8 for el in range(8)] Out[13]: [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] Or similarly, In [21]: [[a for a in range(el*8,(el+1)*8)] for el in range(8)] Out[21]: [[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, 25, 26, 27, 28, 29, 30, 31], [32, 33, 34, 35, 36, 37, 38, 39], [40, 41, 42, 43, 44, 45, 46, 47], [48, 49, 50, 51, 52, 53, 54, 55], [56, 57, 58, 59, 60, 61, 62, 63]]

Lakshman Prasad

Your question is tagged Numpy, so here it is in a form that lets you turn it into a Numpy matrix. from numpy import matrix repeat = 3 width = 7 rows_per_iteration = 3 # The 1st row is also the 4th and 7th, the 2nd is also the 5th and 8th, etc. A = [[0] * width for Null in range(rows_per_iteration)] * repeat A[0][0] = 1 A[1][1] = 1 A[2][2] = 1 A[0][3] = 'c' A[0][4] = 0 A[0][5] = 'a' A[0][6] = 'b' A[1][3] = 'g' A[1][4] = 'd' A[1][5] = 't' A[1][6] = 'f' A[2][3] = 'm' A[2][4] = 'h' A[2][5] = 'u' A[2][6] = 'k' A_mat = matrix(A) If you're going to use them without going to a matrix, repeat = 3 width = 7 rows_per_iteration = 3 total_rows = repeat * rows_per_iteration A = [[0] * width for Null in range(total_rows)] for np in range(0, total_rows, rows_per_iteration): A[np][0] = 1 A[1 + np][1] = 1 A[2 + np][2] = 1 A[np][3] = 'c' A[np][4] = 0 A[np][5] = 'a' A[np][6] = 'b' A[1 + np][3] = 'g' A[1 + np][4] = 'd' A[1 + np][5] = 't' A[1 + np][6] = 'f' A[2 + np][3] = 'm' A[2 + np][4] = 'h' A[2 + np][5] = 'u' A[2 + np][6] = 'k' Which will actually create unique lists for each row instead of reusing them every 3rd row.

agf

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.