0

I have the following code. I have a matrix that I want to take two rows at a time and put into these functions. I've looked through intertools, but I wasn't sure if they would work in this instance. I tried islice, but it didn't give the output I wanted. I know that I have to give the 'methods' the positional arguments, but I've left them out because this code will give results, they are just the same min,max and such over and over. What I was trying was giving me issues about index errors, and objects not being subscriptable. I'm new to python, and I'm working through some books, but they do not cover this directly. I've looked over what some people are looking for on stackoverflow, but they mostly want to read in files 2 rows at a time, not numpy ndarrays. Any suggestions are appreciated.

import numpy as np

def xmax():
    for i in range(1):
        setOne = (matrix[i:1])
        setTwo = (matrix[i+1:2])
         if setOne[i][0:1] > setTwo[i][0:1]:
            xMax = setOne[i][0:1]
         else:
            xMax = setTwo[i][0:1]
         return(xMax)

def xmin():
    for i in range(1):
        setOne = (matrix[i:1])
        setTwo = (matrix[i+1:2])
        if setOne[i][0:1] < setTwo[i][0:1]:
            xMin = setOne[i][0:1]
        else:
            xMin = setTwo[i][0:1]
        return(xMin)

def ymax():
    for i in range(1):
        setOne = (matrix[i:1])
        setTwo = (matrix[i+1:2])
        if setOne[i][1:2] > setTwo[i][1:2]:
            yMax = setOne[i][1:2]
        else:
            yMax = setTwo[i][1:2]
        return(yMax)

def ymin():
    for i in range(1):
        setOne = (matrix[i:1])
        setTwo = (matrix[i+1:2])
        if setOne[i][1:2] < setTwo[i][1:2]:
            yMin = setOne[i][1:2]
        else:
            yMin = setTwo[i][1:2]
        return(yMin)

def zmax():
     for i in range(1):
         setOne = (matrix[i:1])
         setTwo = (matrix[i+2:3])
         if setOne[i][2:3] > setTwo[i][2:3]:
             zMax = setOne[i][2:3]
         else:
             zMax = setTwo[i][2:3]
         return(zMax)

def zmin():
    for i in range(1):
        setOne = (matrix[i:1])
        setTwo = (matrix[i+2:3])
        if setOne[i][2:3] < setTwo[i][2:3]:
            zMin = setOne[i][2:3]
        else:
            zMin = setTwo[i][2:3]
        return(zMin)

 '''***********************************************************************************
Created on Jan 27, 2013

@author: 
***********************************************************************************'''

 f ='/Users/Documents/workspace/findMinMax/crapc.txt'
 x,y,z = np.loadtxt(f, unpack=True, usecols=(1,2,3), ndmin = 2)

 maxZ = max(z)
 minZ = min(z)
 print("Maximum Z value: " + str(maxZ))
 print("Minimum Z value: " + str(minZ))
 matrix = [x,y,z]  
 matrix = np.rot90(matrix)
 matrix = matrix[::-1]




"""for each_row in range(0,len(matrix)-1,2):
        print("Xmax:" + str(xmax()))   
        print("Xmin:" + str(xmin()))
        print("Ymax:" + str(ymax()))
        print("Ymin:" + str(ymin()))
        print("Zmax:" + str(zmax()))
        print("Zmin:" + str(zmin()))"""
2
  • 1) What exactly are you trying to achieve? 2) What the loops over range(1) for --- they only contain a single value, zero. Commented Feb 6, 2013 at 15:37
  • It is indeed unclear what you are trying to do. If you want to minimum or maximum of a numpy ndarray, try using the min and max functions, i.e. numpy.max(z) or even z.max(). The documentation shows how you can take the maximum of each column or row (using the axis keyword). Commented Feb 6, 2013 at 16:32

1 Answer 1

1

It isn't clear to me what you're doing and I don't understand the structure of your code (what you have provided here is not a standalone example), but the indexing in your code is wrong and not how python/numpy works.

The range vec[a:b] takes the b-a elements (not b-a+1!) from vec, starting at index a and ending at index b-1. You seem to be assuming that it will include the element at index b; it doesn't.

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

2 Comments

I will definitely look at the documentation for the numpy.max(). I tried regular max, but as I'm sure you know, it doesn't work. The problem with finding the min and max of an entire column is that I only want to find the min and max of two rows at once. My ndarray has 31 rows and I want to limit my code to only finding the maximums and minimums for the top two rows, then move to the next two(taking the second one from before). For example, I'd want to find the min and max of rows 1 and 2. Then 2 and 3. then 3 and 4.
You can certainly slice the array two rows at a time and then take the maximum, like so: p=np.random.random((31,31)); np.max(p[5:7])

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.