How to extract specific numbers from a matrix? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: How to extract specific numbers from a matrix? (/thread-8759.html) |
How to extract specific numbers from a matrix? - Alberto - Mar-06-2018 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? |