Speed performance of picking unique value from binary matrix file I wrote this code to read a square matrix saved as binary file, that it can be int, uint, float etc... to pick a value from the matrix at given row y and given column x.
Could this code be faster to pick this value, better than 20 seconds.
The maximum row and column number in matrix is 3601 for each one.

import struct

#convert x,y indexes of 2D array into index i of 1D array
#x: index for cols in the 2D
#y: index for rows in the 2D
def to1D(y,x,width):
    i = x + width*y
    return i


def readBinary_as(filename,x,y,file_type,width=3601):
    with open(filename,'rb') as file_data:

        #initialize, how many bytes to be read
        nbByte = 0
        #data type of the file, uint, int, float, etc...
        coding = ''
        if file_type == "signed int":
            nbByte = 2
            coding = '&gt;h'  #2B Signed Int - BE

        if file_type == "unsigned int":
            nbByte = 2
            coding = '&gt;H' #2B Unsigned Int - BE

        if file_type == "unsigned byte":
            nbByte = 1
            coding = '&gt;B' #1B Unsigned Byte - BE

        if file_type == "float":
            nbByte = 4
            coding = '&gt;f' #4B float32 - BE

        #index of my value in 1D array
        i = to1D(y,x,width)
        for each_cell in range(0,i):
            file_data.read(nbByte)

        #read and save the picked value
        my_value_pos = file_data.read(nbByte)
        val = struct.unpack(coding,my_value_pos)[0]
    return val
Try using seek() instead of a loop that reads 1 value at a time.

import io
import struct

#convert x,y indexes of 2D array into index i of 1D array
#x: index for cols in the 2D
#y: index for rows in the 2D
def to1D(y,x,width):
    i = x + width*y
    return i


def readBinary_as(filename,x,y,file_type,width=3601):
    with open(filename,'rb') as file_data:

        #initialize, how many bytes to be read
        nbByte = 0
        #data type of the file, uint, int, float, etc...
        coding = ''
        if file_type == "signed int":
            nbByte = 2
            coding = '&gt;h'  #2B Signed Int - BE

        if file_type == "unsigned int":
            nbByte = 2
            coding = '&gt;H' #2B Unsigned Int - BE

        if file_type == "unsigned byte":
            nbByte = 1
            coding = '&gt;B' #1B Unsigned Byte - BE

        if file_type == "float":
            nbByte = 4
            coding = '&gt;f' #4B float32 - BE

        #index of my value in 1D array
        i = to1D(y,x,width)
        offset = i * nbByte

        # seek to byte offset of desired data
        file_data.seek(offset)

        #read and save the picked value
        my_value_pos = file_data.read(nbByte)
        val = struct.unpack(coding,my_value_pos)[0]
    return val