lundi 13 mars 2023

NumPy Creating two matrices that results in third that calculates diagonal

I am rather new to coding in python but familiar with matlab and Mathematica.

In python, I created two 4x4 matrices (A and B) that have random number elements which in turn creates a third C matrix like:

Matrix C

The diagonal elements should then be presented in 4x2 matrix but I can not construct it right.

This is what I have so far:

import numpy as np
A = np.random.rand(4,4)
B = np.random.rand(4,4)
C = np.array([[A[0,0], A[0,1], B[0,2], B[0,3]],
              [A[1,0], A[1,1], B[1,2], B[1,3]],
              [A[2,0], A[2,1], B[2,2], B[2,3]],
              [A[3,0], A[3,1], B[3,2], B[3,3]]])

My question is, how do I print out the diagonal from Matrix C from here? The output checked in other software should be something like:

C = [[ 1, 2, 3, 4], [ 5, 6, 7, 8], [ 9, 10, 11, 12], [13, 14, 15, 16]]

and then the diagonal

Diagonal = [[1, 6], [10, 15], [3, 8], [12, 16]]




Aucun commentaire:

Enregistrer un commentaire