Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I sum arrays row-wise in Julia?

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

[a, b

c, d]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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].

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading