All,

i have a pretty simple task but for some reason i cannot for the life of me figure out how to do it in Python.

I have a matrix of numbers, this matrix has a variable number of rows depending on the dataset but always 30 columns. first things first i would like to create a new matrix of the same size. in this new matrix i would like the same value, say x, in every row but i different number, say x2 in each column. so each row will read x, x2, x3,...x30.

secondly i would like to multiple each number in the matricies together to form a third matrix, i.e. a(i,j) * b(i,j) = c(i,j)

i am much more used to visual basic programming albeit still not an expert. i made an attempt in Python

for i in range(a.referenceSize())
for j in range (2,30)
b(1,j) = 3000
b(i,j) = exp((log(a(i-1),j))-(0.317598))
i = i + 1
end
end # creates a second matrix, the same size as the input, with log scale 3000 to 0.3

for i in range (a.referenceSize())
for j in range (1,30)
c(i,j) = a(i,j)*b(i,j)
end
end # multiplies each value in matrix a with corresponding value in matrix b

but it comes back with all kinds of errors.

any help you could give would be greatly appreciated

Thanks

Python has a module numpy that will make things easier. However, you can use a Python list of lists for a matrix as shown in this example:

# create a 2D matrix of zeros and populate it

def make_list(size):
    """create a list of size number of zeros"""
    mylist = []
    for i in range(size):
        mylist.append(0)
    return mylist

def make_matrix(rows, cols):
    """
    create a 2D matrix as a list of rows number of lists
    where the lists are cols in size
    resulting matrix contains zeros
    """
    matrix = []
    for i in range(rows):
        matrix.append(make_list(cols))
    return matrix

rows = 5
cols = 10
# create the zero matrices first
mx1 = make_matrix(rows, cols)
mx2 = make_matrix(rows, cols)
mx3 = make_matrix(rows, cols)

# load matrix mx1 with multiples of x
x = 1
for row in range(rows):
    m = 1
    for col in range(cols):
        mx1[row][col] = x*m
        m += 1

# load matrix mx1 with squares of x
for row in range(rows):
    x = 1
    for col in range(cols):
        mx2[row][col] = x*x
        x += 1

# multiply mx1 with mx2
for row in range(rows):
    for col in range(cols):
        mx3[row][col] = mx1[row][col] * mx2[row][col]

# pretty show the different loaded matrix contents
for x in mx1:
    print(x)

print('-'*50)

for x in mx2:
    print(x)

print('-'*50)

for x in mx3:
    print(x)

"""
my result -->
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
--------------------------------------------------
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
--------------------------------------------------
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
"""
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.