Home > Software design >  How can I sum arrays row-wise in Julia?
How can I sum arrays row-wise in Julia?

Time:01-23

Suppose that I have a 2x2 array of the form:

[a, b

c, d]

Here a, b, c and d are numbers.

How can I calculate the column-wise sum in Julia? The result should be the column vector:

[a b

c d].

CodePudding user response:

Assume your data is

mat = [11 12; 21 22 ]
2×2 Matrix{Int64}:
 11  12
 21  22

the column sum is given by

sum( mat , dims = 2 )
2×1 Matrix{Int64}:
 23
 43

and you can get the row sum via sum( mat , dims = 1 ). To produce a vector from that you can do vec( sum( mat , dims = 2 ) ) which gives (2,) that is a vector

  • Related