python - Arrays indexing -


I have two variables, lim1 and lim2, which are indexed to the matrix I define a part of the matrix which is zero Fill:

  matrix [j, lim1: lim2] = 0  

However, in some cases, the MM1 and MM2 exceed the matrix limit That is, they are less than zero or greater than N (where n = len (matrix [j,:]) In these cases, I should use lim1 or lim2 instead of 1 or nm.

The limits of these limits To test, I am currently using the code:

  lim1 = something like Lim 1 <0: lim1 = 0 elif lim1> n: lim1 = N lim2 = some if lim2 & lt; 0: lim2 = 0 elif lim2> n: lim2 = n matrix [j, lim1: lim2] = 0  

Is there any more compact The method is, for example, in IDL it will be written:

  matrix [lim 1> 0  0  n = 0  

This code repeats a lot, so it will be very useful to type in more concise.

Thanks in advance!

You can use minimum and max CAP is a value, so the code can be written a little more concise in this way:

  matrix [j, min (max (0, lim1), n): minimum (max. (0, lim2), n)] = 0  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -