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
在每次迭代中,我使用centersX
函数构造centersY
和linspace
。然后我将它们存储在名为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
,但没有运气。
答案 0 :(得分:0)
你试过用方括号组合它们吗?
也许您也可以使用 matplotlib.pyplot.hist2d。
H[:,:,i], *_ = numpy.histogram2d(xData,yData,bins=[centers[0], centers[1]])
H[:,:,i], *_ = matplotlib.pyplot.hist2d(xData,yData,bins=[centers[0], centers[1]])
在这两种情况下,中心的值是 bin 边缘,而不是中心。您必须调整计算。我觉得去掉dX/2就够了:
centersX(:,i)=linspace(min(xData),max(xData),nbins)';
centersY(:,i)=linspace(min(yData),max(phase),nbins)';