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
  Extract specific sentences from text file Bubly 3 253 May-31-2021, 06:55 PM
Last Post: Larz60+
  How to multiply a matrix with herself, until the zero matrix results peanutbutterandjelly 3 443 May-03-2021, 06:30 AM
Last Post: Gribouillis
  How to extract specific key value pair from string? aditi06 0 355 Apr-15-2021, 06:26 PM
Last Post: aditi06
  finding element of specific field in pandas adjacency matrix amjass12 0 335 Oct-27-2020, 09:27 AM
Last Post: amjass12
  How to extract specific rows and columns from a text file with Python Farhan 0 1,024 Mar-25-2020, 09:18 PM
Last Post: Farhan
  extract specific data from a group of json-files ledgreve 3 1,118 Dec-05-2019, 07:57 PM
Last Post: ndc85430
  Delete specific lines contain specific words mannyi 2 1,441 Nov-04-2019, 04:50 PM
Last Post: mannyi
  How to extract a matrix from .xml.gz file to a excel file or any other output? enyrb 0 663 Oct-21-2019, 01:01 PM
Last Post: enyrb
  Print Numbers starting at 1 vertically with separator for output numbers Pleiades 3 1,338 May-09-2019, 12:19 PM
Last Post: Pleiades
Photo How to Extract Specific Words from PDFs with Python danvsv 1 2,843 Jan-17-2019, 11:07 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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