Ted Desmond
Ted Desmond

Reputation: 57

Adding values to a matrix with a for loop in python

My intention with this program was to get a 4x4 matrix with certain values, but for some reason the loop is putting everything into the same row/column... What is off about my code?

def matrixH0(k):
    H0=[]
    print H0
    for m in range (0,k):
        for n in range (0,k):
            if abs(m-n)==1:
                H0.append(math.sqrt(n+m+1)/2.)
            else:
                H0.append(0)
        print H0

This is my output:

[0,
 0.7071067811865476,
 0,
 0,
 0.7071067811865476,
 0,
 1.0,
 0,
 0,
 1.0,
 0,
 1.224744871391589,
 0,
 0,
 1.224744871391589,
 0]

Upvotes: 0

Views: 13229

Answers (5)

unutbu
unutbu

Reputation: 879133

Append rows to H0, append values to rows:

import math
import pprint
def matrixH0(k):
    H0 = []
    for m in range(k):
        # create a new row 
        row = []               
        for n in range(k):
            if abs(m-n)==1:
                row.append(math.sqrt(n+m+1)/2.)
            else:
                row.append(0)
        H0.append(row)
    return H0
pprint.pprint(matrixH0(4))

yields

[[0, 0.7071067811865476, 0, 0],
 [0.7071067811865476, 0, 1.0, 0],
 [0, 1.0, 0, 1.224744871391589],
 [0, 0, 1.224744871391589, 0]]

By the way, matrixH0 could also be written using nested list comprehensions:

def matrixH0(k):
    return [[math.sqrt(n+m+1)/2. if abs(m-n)==1 else 0 for n in range(k)]
            for m in range(k)]

Upvotes: 2

P. Brunet
P. Brunet

Reputation: 204

I think Raulucco's answer will work. But you if you work with number and matrix, numpy is certainly the module you want.

Your code will look like

import numpy
H0 = numpy.diag([math.sqrt(n + 1) for n in xrange(k - 1)], 1) + \
    numpy.diag([math.sqrt(n) for n in xrange(k - 1)], -1)

Upvotes: 0

khelili miliana
khelili miliana

Reputation: 3822

you find the correct Function

def matrixH0(k):
    H1=[]
    k=4
    print H1
    for m in range (0,k):
        H0=[]
        for n in range (0,k):
            if abs(m-n)==1:
                 H0.append(math.sqrt(n+m+1)/2.)
            else:
                 H0.append(0)
        H1.append(H0)
    return H1

[[0, 0.7071067811865476, 0, 0], [0.7071067811865476, 0, 1.0, 0], [0, 1.0, 0, 1.224744871391589], [0, 0, 1.224744871391589, 0]]

Upvotes: 0

Raulucco
Raulucco

Reputation: 3426

Init the rows at on the first loop and append the numbers to the array under the first loop index

def matrixH0(k):
    H0=[]
    print H0
    for m in range (0,k):
        H0.append([])
        for n in range (0,k):
            if abs(m-n)==1:
                H0[m].append(math.sqrt(n+m+1)/2.)
            else:
                H0[m].append(0)
        print H0

Upvotes: 1

Rob Murray
Rob Murray

Reputation: 1903

You never create a multidimensional array in your code, you just append to a single list. Here is a solution:

def matrixH0(k):
    H0=[]
    print H0
    for m in range (0,k):
        H0.append([])
        for n in range (0,k):
            if abs(m-n)==1:
                H0[m].append(math.sqrt(n+m+1)/2.)
            else:
                H0[m].append(0)
        print H0

Upvotes: 1

Related Questions