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 : -
class Matrix(object):
-
def __init__(self, rows, cols):
-
self.rows = rows
-
self.cols = cols
-
# initialize matrix and fill with zeroes
-
self.data = [[0 for _ in range(cols)] for _ in range(rows)]
-
-
-
-
def __setitem__(self, pos, v):
-
self.data[pos[0]][pos[1]] = v #use b.data[1][2] for getting element starts with 0 row and 0 column
-
-
def __getitem__(self, pos):
-
return self.data[pos[0]][pos[1]]
-
-
def __iter__(self):
-
for row in self.data:
-
yield row
-
-
def __str__(self):
-
outStr = ""
-
for i in range(self.rows):
-
outStr += 'Row %s = %s\n' % (i, self.data[i])
-
return outStr
-
-
def __repr__(self):
-
return 'Matrix(%d, %d)' % (self.rows, self.cols)
-
-
-
import random
-
-
b = Matrix(4,8)
-
-
for row in b:
-
-
for i in range(b.cols):
-
-
row[i] = random.randrange(100)
-
-
-
-
print b
-
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): -
class Matrix(object):
-
def __init__(self, depths, rows, cols):
-
self.rows = rows
-
self.cols = cols
-
self.depths = depths
-
# initialize matrix and fill with zeroes
-
self.data = [[[0 for _ in range(depths)]] [for _ in range(cols)] for _ in range(rows)]
-
-
-
-
def __setitem__(self, pos, v):
-
self.data[pos[0]][pos[1]][pos[2]] = v #use b.data[1][2] for getting element starts with 0 row and 0 column
-
-
def __getitem__(self, pos):
-
return self.data[pos[0]][pos[1]][[pos[2]]
-
-
def __iter__(self):
-
for row in self.data:
-
yield row
-
-
def __depths__(self, depths):
-
for i in range(self.depths):
-
outStr = ""
-
for i in range(self.rows):
-
outStr += 'Row %s = %s\n' % (i, self.data[i])
-
return 'Matrix(%d, %d)' % (self.rows, self.cols)
-
-
-
import random
-
-
b = Matrix(3,4,8)
-
-
for j in range(b.depths):
-
for row in b:
-
for i in range(b.cols):
-
row[i] = random.randrange(100)
-
-
-
-
print b
2 1251 bvdet 2,851
Expert Mod 2GB
This will add depth to your array/matrix: - class Sarray(object):
-
def __init__(self, cols, rows, depth=None):
-
self.cols = cols
-
self.rows = rows
-
self.depth = depth
-
# initialize array and fill with zeroes
-
if depth:
-
self.array = [[[0 for k in range(depth)] for i in range(cols)] for j in range(rows)]
-
else:
-
self.array = [[0 for i in range(cols)] for j in range(rows)]
- >>> a = Sarray(3,4)
-
>>> b = Sarray(3,4,5)
-
>>> a
-
Row 0 = [0, 0, 0]
-
Row 1 = [0, 0, 0]
-
Row 2 = [0, 0, 0]
-
Row 3 = [0, 0, 0]
-
-
>>> b
-
Row 0 = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
-
Row 1 = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
-
Row 2 = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
-
Row 3 = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
-
-
>>>
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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 =...
|
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...
|
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?)
|
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},...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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
| |