0

After calculation of few values and got these values and further I need to calculate more but stuck with how do I do that.

[array([59, 24,  9,  3,  1,  0,  0,  0,  0,  0,  0,  0], dtype=int64),
 array([118,  48,  19,   7,   3,   1,   0,   0,   0,   0,   0,   0],
       dtype=int64),
 array([178,  72,  29,  11,   4,   1,   0,   0,   0,   0,   0,   0],
       dtype=int64),
 array([237,  96,  39,  15,   6,   2,   1,   0,   0,   0,   0,   0],
       dtype=int64),
 array([296, 120,  49,  19,   8,   3,   1,   0,   0,   0,   0,   0],
       dtype=int64),
 array([356, 144,  58,  23,   9,   3,   1,   0,   0,   0,   0,   0],
       dtype=int64),
 array([415, 168,  68,  27,  11,   4,   1,   0,   0,   0,   0,   0],
       dtype=int64),
 array([474, 193,  78,  31,  12,   5,   2,   0,   0,   0,   0,   0],
       dtype=int64),
 array([534, 217,  88,  35,  14,   5,   2,   0,   0,   0,   0,   0],
       dtype=int64),
 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int64),
 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int64),
 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int64)]

Now I need to apply calculation and get an output as

59
142(got it by 24+118)
235(got it by 9+48+178)
331(got it by 3+19+72+237)
429(got it by 1+7+29+96+296)
...
..
.

I'm expecting the above values as my output.

Akilesh
  • 413
  • 3
  • 11
  • Does [this](https://stackoverflow.com/questions/35252993/sum-of-diagonal-elements-in-a-matrix) answer your question? – 0x263A Jul 23 '21 at 07:32

3 Answers3

2

A solution using list comprehension has been provided and certainly produces the desired results. However, based on my timings, it's not very efficient. So, keeping things simple I came up with this:

for i in range(len(a)):
        x = a[0][i]
        r = 1
        for c in range(i - 1, -1, -1):
            x += a[r][c]
            r += 1
        print(f'x={x}')
  • How do I get output for all the values? I'm getting an index error as the values in the list are 12 and data have around 100 values, so how do I do for all 100 values? – Akilesh Jul 26 '21 at 12:45
  • This code will only work on an N*N array as you showed in your original question –  Jul 26 '21 at 13:06
1

try this out:

def find_weird_sum(mtr):

   for i in range(len(mtr)):
       
       intermediate_sum = 0
       
       for j in range(i+1):
           intermediate_sum += mtr[j][i - j]
       
       print(intermediate_sum)

find_weird_sum(x)
theLudo
  • 127
  • 4
0
  • A bad effiency but easy to write code solution.
  • If you need to do it in a large matrix, I can provide more effiency way.

code:

from pandas import array
x = [array([59, 24,  9,  3,  1,  0,  0,  0,  0,  0,  0,  0], dtype="int64"),
 array([118,  48,  19,   7,   3,   1,   0,   0,   0,   0,   0,   0],
       dtype="int64"),
 array([178,  72,  29,  11,   4,   1,   0,   0,   0,   0,   0,   0],
       dtype="int64"),
 array([237,  96,  39,  15,   6,   2,   1,   0,   0,   0,   0,   0],
       dtype="int64"),
 array([296, 120,  49,  19,   8,   3,   1,   0,   0,   0,   0,   0],
       dtype="int64"),
 array([356, 144,  58,  23,   9,   3,   1,   0,   0,   0,   0,   0],
       dtype="int64"),
 array([415, 168,  68,  27,  11,   4,   1,   0,   0,   0,   0,   0],
       dtype="int64"),
 array([474, 193,  78,  31,  12,   5,   2,   0,   0,   0,   0,   0],
       dtype="int64"),
 array([534, 217,  88,  35,  14,   5,   2,   0,   0,   0,   0,   0],
       dtype="int64"),
 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype="int64"),
 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype="int64"),
 array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype="int64")]
for i in range(len(x[0])):
    result = 0
    for line,_ in enumerate(x):
        for row,value in enumerate(x[line]):
            if line+row == i:
                result+= value
    print(result)

result:

59
142
235
331
429
529
628
726
828
335
134
52
  • A more effient way:

code:

for i in range(len(x[0])):
    print(sum([x[j][i-j] for j in range(i+1)]))

result:

59
142
235
331
429
529
628
726
828
335
134
52
leaf_yakitori
  • 2,232
  • 1
  • 9
  • 21
  • What is the more efficient way? I have large values. – Akilesh Jul 23 '21 at 07:38
  • List comprehensions are certainly concise but not necessarily optimal in terms of efficiency. I have run timings on your "efficient" method and can show that my solution (see Answers) runs ~25% faster. It's also easier to read and understand/maintain –  Jul 23 '21 at 09:12
  • @Andy Knight I ran your code for 1000 times, it cost 1.40 sec. My code cost 1.44sec, which is about 2.3% slower than you. Also, if the matrix is 1000*1000, my code cost 0.065sec, your code cost 0.098sec. – leaf_yakitori Jul 23 '21 at 09:55
  • But the matrix is not 1000x1000 - it's 12x12. I put my solution into a discrete function and did the same with yours. In both cases I removed the print(). I executed each function 100,000 times. Durations were 1.271s and 1.684s respectively. Of course these figures will depend on your CPU. I'm running on 3 GHz 10-Core Intel Xeon W –  Jul 23 '21 at 10:12
  • @Andy Knight, OP said he has large values in comment, I think he means to deal with a large matrix. – leaf_yakitori Jul 23 '21 at 10:15
  • Shouldn't there be more than 12 values in my result? The last ones should be 0's right. – Akilesh Jul 26 '21 at 12:32
  • @Akilesh Reddy What exactly desired output you want? You mean you need 12+11 values? – leaf_yakitori Jul 27 '21 at 01:35