Store values obtained by calculating the distance of points in a matrix

Asked

Viewed 35 times

0

I’m taking my first steps python and I want to store a series of values I got in calculating the distance between points in a matrix NxN to then select the values of each line and establish a limit value condition, and set the values of that limit my code was like this:

import numpy as np

n = int(input('numero de elementos = '))
print('-=' *30)
v =np.zeros(n)
X = np.zeros(n)
Y = np.zeros(n)
for i in range(n):
    X[i] = float(input('x ='))
    Y[i] = float(input('y ='))
print(X,Y)
print('-=' *30)

for i in range(0,n):
    for j in range(0,n):
        dist = (((X[i]-X[j])**2)+((Y[i]-Y[j])**2))**(0.5)
        print(f'[{dist}]', end='')
    print()

R=2
for dist in range(0,n):
    if dist<=R:
        mat_distancia = dist
    print(mat_distancia)

1 answer

0

the np.Matrix() method allows creating the matrix, then we can filter by condition within the defined limit.

m = []
for i in range(0,n):
    m.append([])
    for j in range(0,n):
        dist = (((X[i]-X[j])**2)+((Y[i]-Y[j])**2))**(0.5)
        m[i].append(dist)
        print(f'[{dist}]', end='')
    print()

M = np.matrix(m)
R=2
print(M[M < R])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.