Line equation of every two consecutive points in a NumPy array

I want to find the line equation of every two consecutive points (2-dimensional) in a NumPy array. I know how to do it robustly (i.e. using a loop), but I wonder if there's a more sophisticated approach.

Thanks.

Given below is a robust approach:

import numpy as np

a = np.array([[1, 2], [2, 4], [3, 8], [5, 1]])
N = int(max(a.shape))
b = []
for i in range(N - 1):
    x = [a[i,0], a[i + 1,0]]
    y = [a[i,1], a[i + 1,1]]
    b.append(tuple(np.polyfit(x, y, 1)))

print(b)


Read more here: https://stackoverflow.com/questions/67387303/line-equation-of-every-two-consecutive-points-in-a-numpy-array

Content Attribution

This content was originally published by Yonatan at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: