Skip to content

Instantly share code, notes, and snippets.

@jens-andersson-2-wcar
Created August 2, 2024 11:51
Show Gist options
  • Save jens-andersson-2-wcar/a28fc648d94abd27425bb68356728961 to your computer and use it in GitHub Desktop.
Save jens-andersson-2-wcar/a28fc648d94abd27425bb68356728961 to your computer and use it in GitHub Desktop.
Average bearing: one wrong way and two correct ones
import numpy as np
import scipy.stats
# compass bearings in degrees:
data = [2, 356]
# average bearing:
print(np.mean(data)) # == 179.0, aka "wrong"
print(scipy.stats.circmean(data, high=360, low=0)) # == 359.0, aka "correct"
print(np.rad2deg(np.angle(np.mean(np.cos(np.deg2rad(data)) + np.sin(np.deg2rad(data)) * 1j))%(2*math.pi)))
# == 359.0, aka also correct, but achieving the result in an overly complex way
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment