Python Forum
Finding and indexing maximum value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding and indexing maximum value
#1
I have a large grid, but let's say it is 4 cells wide and 3 cells high. Index of the first cell is 0 and it starts bottom left, finishes in the upper right corner with index 11.
Each cell has a value. So we have a df:

cell;value
0;8
1;2
2;1
3;6
4;4
5;6
6;7
7;1
8;2
9;5
10;3
11;5

How do I identify cell that has a max value around itself?

- For corner cells this means that we look at the observed cell and all three around it.
- For middle cells we observe the cell and all 8 around it.

The goal is to find the cell with max value, add to df a new column and to the cell with max value return 1 and all the others 0.

For the upper example this would mean:

cell;value;maxValue
0;8;1
1;2;0
2;1;0
3;6;0
4;4;0
5;6;0
6;7;1
7;1;0
8;2;0
9;5;0
10;3;0
11;5;0

EDIT:
This is the "image" of the grid and its values, respectively.

IMAGE LINK

Anyone? Should I use IF command and go with if (i+1) and (i-1) and ... ?
Reply
#2
I would come up with a list of possible offsets. Loop through the cells. For each cell, loop through the offsets, and get the other cells at those offsets, adding them up. You'll need a check to make sure you're not checking outside the matrix (or a try/except block). Keep track of the highest total and what it's index is as you go along.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Oct-24-2018, 03:00 PM)ichabod801 Wrote: I would come up with a list of possible offsets. Loop through the cells. For each cell, loop through the offsets, and get the other cells at those offsets, adding them up. You'll need a check to make sure you're not checking outside the matrix (or a try/except block). Keep track of the highest total and what it's index is as you go along.

Implemented on matrices it will look something like that:

def maximum_around(matrix, r, c):
    """Find maximum value with indices around value in matrix."""
    
    values = list()
    coordinates = list()
    
    offset = (0, 1, -1)
    indices = ((i, j) for i in offset for j in offset)
    
    next(indices, None)
    
    for rec in indices:
    
        try:
            row = r - 1 + rec[0]
            col = c - 1 + rec[1]
            
            if 0 <= row and 0 <= col:
                value = matrix[row][col]
                values.append(value)
                coordinates.append([row, col])
    
        except IndexError: 
            continue
        
    return {max(values): coordinates[values.index(max(values))]}
  • offsets defines relative changes needed for given indice to get surrounding elements (incl position of given indice himself because relative change [0][0] results in no change in indice)
  • due to order of elements in offset, indices generator object created in such way that first item is (0, 0). First item is consumed by next()
  • looping through generator object remaining elements, calculating and assigning values to matrix indices and adding values and coordinates to lists if indices are not negative (we don't want have row[-1]) and continuing when indices are out of range (IndexError)
  • function expects that user will enter cell location in finger-based index and deduction -1 is needed for converting values to 0-based index. It can be easily changed.

>>> n = [[-1, -2, -3],
         [-4, -5, -6],
         [-7, -8, -9]
        ]

>>> maximum_around(n, 3, 3)
{-5: [1, 1]}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Moving data from one Excel to another and finding maximum profit azizrasul 7 1,458 Oct-06-2022, 06:13 PM
Last Post: azizrasul
  How to change 0 based indexing to 1 based indexing in python..?? Ruthra 2 4,310 Jan-22-2020, 05:13 PM
Last Post: Ruthra
  Finding all maximum values in a matrix Lightning1800 3 3,805 May-14-2018, 03:55 PM
Last Post: Lightning1800

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020