0

I have two matrices. The calculation of the sum over axis=1. Then I want to transpose the obtained result. The result incorporates in expression. You must be elements of the matrix 1 and 2 calculates the sum, row by row.

Do you need to transpose the array of sum or is there some other way?

matrix1 = [[ 5.  4.  3.  5.  3.]
           [ 5.  7.  8.  2.  2.]
           [ 8.  2.  4.  0.  3.]
           [ 7.  2.  5.  3.  5.]
           [ 3.  1.  3.  0.  2.]]

matrix2 = [[  5.   7.   6.   5.   4.]
           [  3.  45.   2.   3.   4.]
           [  2.   4.   6.   4.   3.]
           [  3.   4.   5.   6.  54.]
           [  4.   3.   6.   7.   5.]]

s_1 = np.array(matrix_1.sum(axis=1))
s_1 = np.array(matrix_2.sum(axis=1))

s_1T = np.transpose(s_1)
s_2T = np.transpose(s_2)

Result:

S_1T
[ 20.  24.  17.  22.   9.]

S_2T
[ 27.  57.  19.  72.  25.]

How to transpose the array?

FROM
[ 20.  24.  17.  22.   9.]
[ 27.  57.  19.  72.  25.]

TO
[20.
 24.
 17.
 22.
  9.]

 [27.
  57.
  19.
  72.
  25.]

Transposed array using in the expression:

result = ((matrix_2 - matrix_1)/matrix_1)/((s_2T-s_1T)/s_1T)
Alex Riley
  • 169,130
  • 45
  • 262
  • 238
kamfulebu
  • 229
  • 1
  • 3
  • 11

2 Answers2

1
s1_t = map(lambda e: [e], s_1)

Most likely it's answer you seek.

iced
  • 1,562
  • 8
  • 10
1

You're losing an axis when you take the sum and getting back a one-dimensional array. Transposing has no effect on one-dimensional arrays.

You can either insert a new axis, or you can specify keepdims=True when you take the sum to maintain two dimensions (and avoid the need to transpose the array):

matrix1.sum(axis=1, keepdims=True)

returns:

np.array([[20.],
          [24.],
          [17.],
          [22.],
          [ 9.]])
Alex Riley
  • 169,130
  • 45
  • 262
  • 238