简体   繁体   中英

How can iterate over a 2-D matrix in python with respect to coloumns and rows?

I have this mathematical formula that I have to implement in python :

在此处输入图片说明

Basically, N^AC is an incidence matrix where I want to find all those indices where 'j' element is present NAC(j), Multiply it with the element xij(dirac's delta), which is present at ith coloumn and jth row. The summation is from 1 to p, where p is the enumber of coloumns = 739 in this case.

The second part is the same, but the summation is now over another k as well, and where NAC(jk) is where both j and k are present.

Can anyone please help me with this!! Please.

You can initialize an array with

rows = 2
cols = 5
a = np.zeros((rows, cols), float)

This prints a as:

array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

Then you can access your elements as following:

row_index = 1
col_index = 3
a[row_index, col_index] = 5

The resulting a is:

array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  5.,  0.]])

As you can see in python the row_index goes from 0 .. rows-1 .

It think with this knowledge you should be able to implement your formula.

Let us know how it goes and where you need more help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM