I'm new to python so I need some help with this:
I have 2D array of numbers that represent density of a circle of material in a space and I want to locate the centre. So I want to get the index of the numbers representing the diameter and then the middle index will be the centre. In this code I only store the value of the density : tempdiameter.append(cell) I want the index of the cell itself. How can I do that. Also I don't want to use lists for diameter. so how can I create a dynamic 1D np array? Thanks
for row in x:
for cell in row:
if cell!=0:
tempdensity+=cell
tempdiameter.append(cell)
if tempdensity>maxdensity:
maxdensity=tempdensity
if len(tempdiameter)>=len(diameter):
diameter=tempdiameter
tempdensity=0
tempdiameter=[]
To get the row with the highest number of non-zero cells and the highest sum you can do
densities = x.sum(axis=1)
lengths = (x > 0).sum(axis=1)
center = x[(densities == densities.max()) & (lengths == lengths.max()]
Try to avoid using loops in numpy
. Let me know if this isn't what you want and I'll try to answer it better. You should provide sample input/output when asking a question. You can also edit your question rather than adding comments.