Match and mark the positions of elements of a vector along a given axis of a numpy array -
i want mark maxima along given axis of array (which shape may n-dimensional), works fine along first one, rest can't figure out. don't want iterate on axis, because there can arbritarily many of them.
>>> = range(5)*3 >>> = array(a).reshape([3,5], order='f') >>> array([[0, 3, 1, 4, 2], [1, 4, 2, 0, 3], [2, 0, 3, 1, 4]]) >>> b = amax(a, axis= 0) >>> c = amax(a, axis= 1) >>> b == array([[false, false, false, true, false], [false, true, false, false, false], [ true, false, true, false, true]], dtype=bool) this want for:
>>> c == false but (of course) not.
how working?
i came with:
def tiletuple(t,axis): m = [1]*t.ndim m[axis] = t.shape[axis] return m ax = 1 tile(expand_dims(amax(a, axis=ax), axis=ax), tiletuple(a, ax)) ==
Comments
Post a Comment