user3495725
user3495725

Reputation: 21

building an nxn matrix in python numpy, for any n

Is it possible using numpy for python (versions 3.3) to write the code for building an nxn matrix, without specifying n? I need to index the entries as A_i,j or something like that, but I dont even know how to define the A_i,j so that they are actually objects. I thought something like this might work:

n    
i=1
j=1

when i (is less than) n+1

  when j (is less than) i+1
   A_i,j= f(i,j)
   j+=1

i+=1

but this does not work...any suggestions? My ultimate Goal is to write the QR decomposition for an arbitrary nxn matrix. But I need to know how to define the matrix that I am working on first. I am very new to python and thus numpy and so dont know much of anything. any help would be greatly appreciated. I am also new to stackexchange so sorry for the bad piece of code i have there. (is less than) is supposed to mean the triangle sign missing the base with head pointing to the left- that is the obvious less than symbol

Upvotes: 2

Views: 7801

Answers (1)

jonrsharpe
jonrsharpe

Reputation: 121973

You can create an empty nxn array:

import itertools
import numpy as np

my_array = np.empty([n, n])

Then set the value at coordinate i, j to f(i, j).

for i, j in itertools.product(range(n), range(n)):
    my_array[i, j] = f(i, j)

Upvotes: 4

Related Questions