【发布时间】:2016-08-23 23:28:28
【问题描述】:
for i=1:n
centersX(:,i)=linspace(min(xData)+dX/2,max(xData)-dX/2,nbins)';
centersY(:,i)=linspace(min(yData)+dY/2,max(phase)-dY/2,nbins)';
centers = {centersX(:,i),centersY(:,i)};
H(:,:,i) = hist3([xData yData],centers);
end
在每次迭代中,我使用linspace 函数构造centersX 和centersY。然后我将它们存储在一个名为 centers 的 2x1 单元数组中。 H 是一个 nbins X nbins X n 结构。在每次迭代中,我用 hist3 中的数据填充 H 的 nbins X nbins 切片。
我正在寻找 Python 等价物。我在传递 numpy.histogram2d 的参数时遇到问题:
H[:,:,i] = numpy.histogram2d(xData,yData,centers)
我收到以下错误:
Traceback (most recent call last):
line 714, in histogramdd
N, D = sample.shape
AttributeError: 'list' object has no attribute 'shape'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
line 36, in <module>
H[:,:,i] = numpy.histogram2d(xData, yData, centers)
line 714, in histogram2d
hist, edges = histogramdd([x, y], bins, range, normed, weights)
line 718, in histogramdd
N, D = sample.shape
ValueError: too many values to unpack (expected 2)
由于 Python 没有元胞数组,我将中心更改为数组数组,其中 centers[0] = centersX 和 centers[1] = centersY。我需要改变什么,以假设 matlab 和 python 之间的数据相同,输出将匹配?
编辑:
我也尝试过H[:,:,i] = numpy.histogram2d(xData,yData, bins=(centersX,centersY)) 将合并步骤剪切为centers,但没有运气。
【问题讨论】:
-
xData、yData、centersX、centersY的类型有哪些? -
@Eric float64。使用 numpy 构造函数后,我没有将它们转换为任何东西
-
所以他们是
ndarrays?什么.shape? -
是
ndarray的.shape(nbins,n)
标签: python matlab numpy histogram