473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

regarding arrays/matrices...

hey all... im using python 2.5 with Tkinter (if any1 wants 2 know)...

earlier i had queried about how to make a matrix in this forums and i got an excellent reply in which the code for making a 2d matrix was as :

Expand|Select|Wrap|Line Numbers
  1. class Matrix(object):
  2.     def __init__(self, rows, cols):
  3.         self.rows = rows
  4.         self.cols = cols
  5.         # initialize matrix and fill with zeroes
  6.         self.data = [[0 for _ in range(cols)] for _ in range(rows)]
  7.  
  8.  
  9.  
  10.     def __setitem__(self, pos, v):
  11.         self.data[pos[0]][pos[1]] = v              #use b.data[1][2] for getting element starts with 0 row and 0 column 
  12.  
  13.     def __getitem__(self, pos):
  14.         return self.data[pos[0]][pos[1]]
  15.  
  16.     def __iter__(self):
  17.         for row in self.data:
  18.             yield row
  19.  
  20.     def __str__(self):
  21.         outStr = ""
  22.         for i in range(self.rows):
  23.             outStr += 'Row %s = %s\n' % (i, self.data[i])
  24.         return outStr
  25.  
  26.     def __repr__(self):
  27.         return 'Matrix(%d, %d)' % (self.rows, self.cols)
  28.  
  29.  
  30. import random
  31.  
  32. b = Matrix(4,8)
  33.  
  34. for row in b:
  35.  
  36.     for i in range(b.cols):
  37.  
  38.          row[i] = random.randrange(100)
  39.  
  40.  
  41.  
  42. print b
  43.  
the above works very well for 2d matrix... but im stuck for a 3d matrix... suppose i introduce variable "depths"... and try 2 write a code for making a 3d matrix... i can at best come up with... (but it doesnt work... python gives error while running):

Expand|Select|Wrap|Line Numbers
  1. class Matrix(object):
  2.     def __init__(self, depths, rows, cols):
  3.         self.rows = rows
  4.         self.cols = cols
  5.         self.depths = depths
  6.         # initialize matrix and fill with zeroes
  7.         self.data = [[[0 for _ in range(depths)]] [for _ in range(cols)] for _ in range(rows)]
  8.  
  9.  
  10.  
  11.     def __setitem__(self, pos, v):
  12.         self.data[pos[0]][pos[1]][pos[2]] = v              #use b.data[1][2] for getting element starts with 0 row and 0 column 
  13.  
  14.     def __getitem__(self, pos):
  15.         return self.data[pos[0]][pos[1]][[pos[2]]
  16.  
  17.     def __iter__(self):
  18.         for row in self.data:
  19.             yield row
  20.  
  21.     def __depths__(self, depths):
  22.             for i in range(self.depths):
  23.                     outStr = ""
  24.                     for i in range(self.rows):
  25.                         outStr += 'Row %s = %s\n' % (i, self.data[i])
  26.                         return 'Matrix(%d, %d)' % (self.rows, self.cols)
  27.  
  28.  
  29. import random
  30.  
  31. b = Matrix(3,4,8)
  32.  
  33. for j in range(b.depths):
  34.     for row in b:
  35.         for i in range(b.cols):
  36.             row[i] = random.randrange(100)
  37.  
  38.  
  39.  
  40. print b
Dec 20 '07 #1
2 1251
bvdet
2,851 Expert Mod 2GB
This will add depth to your array/matrix:
Expand|Select|Wrap|Line Numbers
  1. class Sarray(object):
  2.     def __init__(self, cols, rows, depth=None):
  3.         self.cols = cols
  4.         self.rows = rows
  5.         self.depth = depth
  6.         # initialize array and fill with zeroes
  7.         if depth:
  8.             self.array = [[[0 for k in range(depth)] for i in range(cols)] for j in range(rows)]
  9.         else:
  10.             self.array = [[0 for i in range(cols)] for j in range(rows)]
Expand|Select|Wrap|Line Numbers
  1. >>> a = Sarray(3,4)
  2. >>> b = Sarray(3,4,5)
  3. >>> a
  4. Row 0 = [0, 0, 0]
  5. Row 1 = [0, 0, 0]
  6. Row 2 = [0, 0, 0]
  7. Row 3 = [0, 0, 0]
  8.  
  9. >>> b
  10. Row 0 = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
  11. Row 1 = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
  12. Row 2 = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
  13. Row 3 = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
  14.  
  15. >>> 
Dec 20 '07 #2
STM
4
try using NumPy instead of coding your own matrix class.

http://numpy.scipy.org/

it provides a fully functional array object.
just to give you an example, to create a 3X3 matrix you d only have to write 2 lines of code;

from numpy import array
#creates an empty matrix(i.e. multidimensionalarray)
matrixx=array((3,3))


if you wanted to have your matrix 3x3 initiazialized:

from numpy import ones
#creates a matrix with all ones
matrix_1=ones((3,3))

#creates a matrix with all zeros
matrix_0=zeros((3,3))


just to give you a couple of examples.
plus you d have a whole set of ready-to-use operations.
Dec 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Jack Liddle | last post by:
Hi there I'm using Numerical Python to handle my arrays but I'm not sure how to accomplish the following. I want a NxNxNxN array where each element of the the array (A) is itself a 3x3 array....
6
by: fivelitermustang | last post by:
I have two matrices allocated dynamically in both directions: matrix x and matrix v. I want to pass these matrices into a function by reference. What I have written down isn't working... can...
11
by: fivelitermustang | last post by:
Actually, how would I go about allocating a four-dimensional dynamic array? I only know how to make two dimensional dynamic arrays: double **v; v = new double*; for (int i=0; i<C; i++) { v =...
8
by: sam_cit | last post by:
Hi, I came to read this particular statement with respect to reducing the complexity of a code, The usage of functional maps matrices instead of switch statements should be considered. I...
3
by: alcabo | last post by:
Hello, I'd like to improve several critical routines involving arrays (vectors and matrices)... How are arrays stored in memory? Row major or column major? (Like in C or like Fortran?)
8
by: kd | last post by:
Newbie question here. It's been a while since I've done C programming, and I hit a wall last night. Let's say I have a three dimensional array, like so: int p = {{{0,0,0}, {1,1,1},...
38
by: djhulme | last post by:
Hi, I'm using GCC. Please could you tell me, what is the maximum number of array elements that I can create in C, i.e. char* anArray = (char*) calloc( ??MAX?? , sizeof(char) ) ; I've...
7
by: abunn | last post by:
I've been learning Visual C++ on the fly at work for the last 2 weeks and could use some help. I'm writing a program to read a .raw file which has a standard format of 9 columns and then depending...
1
by: xHolyWrath | last post by:
Hi everyone! I'm new to this forum so any advise will be very much appreciated. I'm currently creating a C program that would read a text file full of integer values that represents pixel spectrum...
1
by: csgirlie | last post by:
I'm trying to store or arrange three sets of two-dimensional data into three 2xN matrices that are stored as NumPy arrays. import os # for file handling functions import numpy as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.