Python Forum
How to extract specific numbers from a matrix?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to extract specific numbers from a matrix?
#1
Dear Python Users,

I have the following matrix:

a = array([[100.        , 100.        , 100.        ],
   [175.24163615, 198.31220183,  32.71406333],
   [150.36778472,  82.62453566,  38.51860719],
   [ 155.28247936,  191.50455896,  23.09254829],
   [ 36.4302652 ,  67.34580186,  30.82481977]])
What I do is that I check each column and if the number in the third row is greater then 100 I change it into 21.75; if the number in the fourth row is greater then 100 I change it into 29. For this, I use the following code:

Z1[2]=[21.75 if x>100 else x for x in Z1[2]]
Z1[3]=[29 if x>100 else x for x in Z1[3]]
What I am struggling with is to do the following. Once there is a change, I want to create an array with the changed number (only the first one in a given column) and if there is no change take the last one of a given column. Say, for the matrix above by running the code I will receive:

a = array([[100.        , 100.        , 100.        ],
   [175.24163615, 198.31220183,  32.71406333],
   [21.75,  82.62453566,  38.51860719],
   [ 29,  29,  23.09254829],
   [ 36.4302652 ,  67.34580186,  30.82481977]])
So, the final array should look like:

a = array([21.75, 29, 30.82481977])
The problem is that I can not directly set a condition to append an array with a number that is equal to 21.75 or 29, since 1st I need to ensure just the first number changed and in my working matrix (the above is just an example) there are some numbers 21.75 and 29 that I randomly defined without being changed by imposed conditions.
Can anyone help me with this?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Check if two matrix are equal and of not add the matrix to the list quest 3 778 Jul-10-2023, 02:41 AM
Last Post: deanhystad
  Using locationtagger to extract locations found in a specific country/region lord_of_cinder 1 1,222 Oct-04-2022, 12:46 AM
Last Post: Larz60+
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,225 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  nsimplify, make zero the really small numbers in the matrix quest 5 1,588 Jan-19-2022, 11:41 PM
Last Post: quest
  Build a matrix by pressing buttons of an interface in Tkinter which extract data from juandiegopulla 1 1,897 Sep-13-2021, 07:28 PM
Last Post: deanhystad
  Extract specific sentences from text file Bubly 3 3,338 May-31-2021, 06:55 PM
Last Post: Larz60+
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 3,302 May-03-2021, 06:30 AM
Last Post: Gribouillis
  How to extract specific key value pair from string? aditi06 0 2,485 Apr-15-2021, 06:26 PM
Last Post: aditi06
  finding element of specific field in pandas adjacency matrix amjass12 0 1,640 Oct-27-2020, 09:27 AM
Last Post: amjass12
  How to extract specific rows and columns from a text file with Python Farhan 0 3,350 Mar-25-2020, 09:18 PM
Last Post: Farhan

Forum Jump:

User Panel Messages

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