faridCS227
faridCS227

Reputation: 37

I am stuck at the process of retrieving a value from a matrix

I am stuck at the process of retrieving a value from a matrix. I am using a MatLab program as reference. for example. delv(2,k) = dell{2,K}(1,1). Which mean, the value of delv(2,k) is the value from the 1st column and 1st row of matrix dell{2,K}. I'm using np.matrix and I'm stuck in retrieving the value for 1st row 1st column from dell(2,k).

def ww(j,k):
    return npy.matrix.I(alfa(j,k))*(rr(j,k)-(BJ(j,k)*ww(j-1,k)))

def dell(j,k):
     if j == np:
        return ww(np,k)
     else:   
        return ww(j,k) - (gamma(j,k)*dell(j+1,k))
def delf(j,k):
    if j == 1:
        return 0
    elif j == 2:
        # This should be returning the 2nd row 1st column value of dell(2,k)
        return dell(2,k) (2,1) 
    else:
        return dell(j,k)
def delu(j,k):
    if j == 1 or j == np:
        return 0
    elif j == np-1:
        return dell(j,k)
def delv(j,k):
    if j == 1:
        return dell(2,k)
    elif j == 2:
        return dell(2,k)
    else:
        return dell(j,k)

Upvotes: 1

Views: 120

Answers (1)

askewchan
askewchan

Reputation: 46530

Instead of:

return dell(2,k) (2,1) 

You should use:

return dell(2, k)[1,1]

The difference being that you should use [] instead of () to get the [row, col] value of an array or matrix. Note that [1,1] is actually the second row and second column:

In [201]: a = npy.array([[1,2],[3,4]])

In [202]: a
Out[202]: 
array([[1, 2],
       [3, 4]])

In [203]: a[1,1]
Out[203]: 4

In [204]: a[0,0]
Out[204]: 1

In [205]: a[0,1]
Out[205]: 2

You can access an entire row or col as such:

# the first row
In [206]: a[0]
Out[206]: array([1, 2])

# the second row
In [207]: a[1]
Out[207]: array([3, 4])

# the second column
In [208]: a[:,1]         # the : gives all rows here, the 1 gets second column
Out[208]: array([2, 4])

# the first row again, using a `:` even though it's not required
In [209]: a[0,:]         # here the : gives all columns (it can be left off as in line 206)
Out[209]: array([1, 2])

Upvotes: 2

Related Questions