假设我有2个矩阵M和N(都有> 1列).我还有一个索引矩阵I,有2列 - 1表示M,1表示N.N的索引是唯一的,但M的索引可能出现不止一次.我想要执行的操作是,
for i,j in w:
M[i] += N[j]
Run Code Online (Sandbox Code Playgroud)
除了for循环之外,还有更有效的方法吗?
我有一个用HEALPY制作的HealPix图,如Healpy:从数据到Healpix图(像素较少,例如,nside = 2,请参见下面的代码)。
import healpy as hp
import numpy as np
import matplotlib.pyplot as plt
# Set the number of sources and the coordinates for the input
nsources = int(1.e4)
nside = 2
npix = hp.nside2npix(nside)
# Coordinates and the density field f
thetas = np.random.random(nsources) * np.pi
phis = np.random.random(nsources) * np.pi * 2.
fs = np.random.randn(nsources)
# Go from HEALPix coordinates to indices
indices = hp.ang2pix(nside, thetas, phis)
# Initate the map and fill it …Run Code Online (Sandbox Code Playgroud)