0

below is the code to create multiple sets of 0's and fill it with random numbers, im then taking the first column and calculating the maximum value, the problem is I need to be able to do this for every row (i.e rows 2 through 10) ive tried putting it into a for loop however it doesnt work with the traditional y=1, mx[:,y], y = y+1

can anyone offer some help? cheers

import pylab

mx = pylab.zeros ((10,6))

for j in range(0,10):
    mx[j] = pylab.randn()

p = mx[:,1]
a = max (p)
1
  • Regarding your recent edit, if you have a compelling reason for that, please flag your question (click flag then select other) and let us know. I'm locking it for now. Commented Jan 21, 2012 at 17:06

2 Answers 2

1

If you are looking for the max of rows 2 through 10, then use mx.max(axis=1) to find the max of all rows, and then slice it down to just rows 2 through 10:

mx.max(axis=1)[2:]

For example, if

In [38]: mx = np.arange(60).reshape((10, 6))

In [39]: mx
Out[39]: 
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35],
       [36, 37, 38, 39, 40, 41],
       [42, 43, 44, 45, 46, 47],
       [48, 49, 50, 51, 52, 53],
       [54, 55, 56, 57, 58, 59]])

Then

In [40]: mx.max(axis=1)[2:]
Out[40]: array([17, 23, 29, 35, 41, 47, 53, 59])

Finally, as larsman has already shown, you can use mx = np.random.randn(10, 6) to make the random matrix.

Sign up to request clarification or add additional context in comments.

2 Comments

this works perfectly very much, Im now at the stage of import pylab mx=pylab.arange(60) mx = pylab.randn (10,6)>print mx y = mx.max(axis=1) print y
which produces the maximum values in each column , I now need to work out how to calculate the probability of a new maximum value being reached, i.e in the first column there is a 100% chance of a new maximum value, in the second column there is a 50% chance of a new maximum value etc. The only way to do this is to have code which tells me how many of the values in the 2nd column are greater than the maximum value of the first column, etc, any ideas? @unutbu
1
import numpy as np
mx = np.random.randn(10, 6)
np.max(mx, axis=0)

I've taken the library of using NumPy instead of Pylab; that's what Pylab uses internally, anyway. You can also use pylab.amax instead of np.max, that's exactly the same function.

2 Comments

this only prints out the first column maximum value a whole bunch of times, I need the first maximum value from column 1, and then the maximum value from column 2, and then the third etc (assumin the values continue to become higher than the previous columns) any ideas?
@user1114835: if this "prints out the first column maximum value a whole bunch of times", then either you got unlucky with your random numbers, or you made a mistake in copy-pasting. This code gives you exactly what you want, except for the "assuming" part; you'll need to write a loop for that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.