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

Aggregate parts of a column in a matrix

I have a matrix like this one:

A = [[ 1.0,  2.0,  3.0,  4.0,  5.0],
     [ 6.0,  7.0,  8.0,  9.0, 10.0],
     [11.0, 12.0, 13.0, 14.0, 15.0],
     [16.0, 17.0, 18.0, 19.0, 20.0]]

And i want to aggregate parts of the matrix. For example the mean of 6 [1][0] and 11 [2][0].
My intuitiv idea was doing it like this: np.mean(A[1:2][0]). But this doesn’t work.

How can I aggregate intervals of a matrix?

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

>Solution :

You can use here Numpy to convert in in Numpy array

A = [[ 1.0,  2.0,  3.0,  4.0,  5.0],
     [ 6.0,  7.0,  8.0,  9.0, 10.0],
     [11.0, 12.0, 13.0, 14.0, 15.0],
     [16.0, 17.0, 18.0, 19.0, 20.0]]

import numpy as np
slice = np.array(A)[1:3,0]
mean = np.mean(slice)

In this example, we use the slicing notation A[1:3, 0] to select rows 1 to 2 (exclusive) and column 0 of matrix A. This creates a 1D array [6.0, 11.0], and then we calculate the mean using np.mean(), which gives us the result of 8.5.

Remember that when slicing arrays, the range is exclusive for the end index. So 1:3 selects rows 1 and 2, but not 3. Similarly, 0 selects only column 0.

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