Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
math,numpy
#1
I need write a function which generate a random matrix (bigger than 2x2). And calculate all the 2x2 part determinant of the generated matrix and generate a new matrix with that values. Confused


that's my code:
arr = np.random.randint(1,10,(3,3))
matrix = np.zeros((3,3))
matrix[0][0] = int(np.linalg.det(arr[0:2,0:2]))
matrix[0][1] = int(np.linalg.det(arr[0:2,1:3]))
matrix[1][0] = int(np.linalg.det(arr[1:3,0:2]))
matrix[1][1] = int(np.linalg.det(arr[1:3,1:3]))
how can i edit this to work for exaple on a 4x4 or on a 16x16 matrix?
Larz60+ write Apr-16-2021, 08:27 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time, please use bbcode tags on future posts.
Reply
#2
I suggest
b = a[:-1,:-1] * a[1:,1:] - a[:-1,1:] * a[1:,:-1]
For example
>>> import numpy as np
>>> a = np.random.randint(1, 10,(5,6))
>>> b = a[:-1,:-1] * a[1:,1:] - a[:-1,1:] * a[1:,:-1]
>>> print(a)
[[4 1 8 9 4 4]
 [3 6 7 8 9 1]
 [9 4 8 6 5 7]
 [9 7 1 8 4 1]
 [1 4 9 5 4 7]]
>>> print(b)
[[ 21 -41   1  49 -32]
 [-42  20 -22 -14  58]
 [ 27 -52  58 -16 -23]
 [ 29  59 -67  12  24]]
Reply
#3
Thanks for your solution. Big Grin It works perfectly.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020