Posts: 33
Threads: 13
Joined: Jul 2017
Hello folks,
I need a small help , it is very easy question but since I am beginner I stucked a bit.
I have a matrix like that :
C=[[1 2 3 4 ]
[7 8 9 10]
[12 13 14 15]
[5 7 7 6]]
I need to check all the elements of third line of matrix [12 13 14 15]:
If first element is > b:
do this
with the same fashion from first to fourth which is 15 , I have to check. I could not select specifically third line of matrix to check the elements ? How can I do that ?
Thank you
Posts: 232
Threads: 2
Joined: Sep 2017
Sep-16-2018, 02:55 PM
(This post was last modified: Sep-16-2018, 02:55 PM by gruntfutuk.)
Try this, should help you figure it out:
C=[[1, 2, 3, 4 ],
[7, 8, 9, 10],
[12, 13, 14, 15],
[5, 7, 7, 6]]
for row in C:
print(row)
for element in row:
print(element)
print(C[3])
print(C[3][2])
I am trying to help you, really, even if it doesn't always seem that way
Posts: 33
Threads: 13
Joined: Jul 2017
Sep-16-2018, 03:16 PM
(This post was last modified: Sep-16-2018, 03:17 PM by juniorcoder.)
Hello thank you ,
but how can I read the third line of matrix one by one 12,13,14,15?
what I am trying to do:
if 12>a:
do this
if 13>b:
do this
etc
Posts: 232
Threads: 2
Joined: Sep 2017
That is shown in the code I shared. You need to try it and experiment.
I am trying to help you, really, even if it doesn't always seem that way
Posts: 33
Threads: 13
Joined: Jul 2017
Hello,
I have an easy question. I have an matrix like
C=[[1 2 3]
[4 5 6]
[7 8 9]
and I have a list L=[5 10 5]
I need to check if the elements of first line is less than 5 (L[0]), if the elements of second line is less than 10(L[1]) and if the elements of last line is less than 10(L[2])
And I need the combine this check with other condition :
Let's say
if a>b and "the controls above"
How can I write it ?
Thank you so much
Posts: 12,053
Threads: 488
Joined: Sep 2016
Sep-17-2018, 12:08 PM
(This post was last modified: Sep-17-2018, 12:08 PM by Larz60+.)
how to you think it should be written? Give it a go.
Posts: 33
Threads: 13
Joined: Jul 2017
Sep-17-2018, 12:10 PM
(This post was last modified: Sep-17-2018, 12:15 PM by juniorcoder.)
I had thought :
if (a<=b and all(C[i][n]<=L[i]) for i in range(T)for n in range(N)):
Do this
but it did not work
Posts: 12,053
Threads: 488
Joined: Sep 2016
>>> c=[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> sum(c[0]) < 5 and (sum(c[1]) < 10 and sum(c[2]) < 10)
False
>>>
Posts: 33
Threads: 13
Joined: Jul 2017
But we do not know the content of list L=[5 10 5] I just gave an example like that: I should use the index of L to for the right hand side of inequlaity. and the other question how will I build the if statement with the first condition ?
Than you
Posts: 566
Threads: 10
Joined: Apr 2017
Numpy arrays are intended to be used as objects - looping is wasteful and inefficient. This is how you can compare by rows and by columns
Output: In [89]: table = numpy.array(range(1, 10)).reshape(3, 3)
In [90]: table
Out[90]:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
In [91]: compare_to = numpy.array([2, 5, 6])
In [92]: compare_to
Out[92]: array([2, 5, 6])
In [93]: # Compare by columns
In [94]: table < compare_to
Out[94]:
array([[ True, True, True],
[False, False, False],
[False, False, False]])
In [95]: # Compare by rows
In [96]: table < compare_to.reshape(3, 1)
Out[96]:
array([[ True, False, False],
[ True, False, False],
[False, False, False]])
numpy is a complex package - going into it without learning Python is a bad idea, and using it without learning is an exercise in futility
Test everything in a Python shell (iPython, Azure Notebook, etc.) - Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
- Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
- You posted a claim that something you did not test works? Be prepared to eat your hat.
|