【问题标题】:Normalize 3d histogram so the sum under the curve = 1 in Matlab归一化 3d 直方图,使曲线下的总和 = 1 在 Matlab
【发布时间】:2014-12-30 16:18:54
【问题描述】:

我需要了解如何制作我已标准化的数据的 3d 直方图,因此 =1 下的区域。 我有

data=[data_x,data_y];
[HIST,Cent]=hist3(data];

我已阅读以下帖子:

MatLab: Create 3D Histogram from sampled data

但我仍然无法理解该方法。哪位高手可以帮忙解释一下如何在 Matlab 中做到这一点?

编辑:

我使用了以下代码:

load('of.mat')
data=[single(theta(:)),mag(:)];
%define the x and y axis
edges{1} = -180:90:180;
edges{2} = 0:0.2:1;
hist3(data, 'Edges',edges);
[N,C]  = hist3(data, 'Edges',edges);
x_diff = diff(edges{1});
y_diff = diff(edges{2});
x = repmat([x_diff, x_diff(end)], length(edges{2}),1)';
y = repmat([y_diff, y_diff(end)], length(edges{1}),1);
% volume of the histogram
V_tot  = sum(sum(x.*y.*N));
N_norm = N/V_tot;
figure
% plot normalized histogram
bar3(-C{2}, N_norm');
axis normal

效果很好,但是如何更改归一化直方图上的轴抽动,它的负数和我的数据应该是正数。我的 data_x 介于 -180 和 180(角度)之间,而 data_y 介于 0 和 1 之间。我无法发布图像。

【问题讨论】:

    标签: matlab 3d histogram2d


    【解决方案1】:

    试试这个代码,如果箱子被平均放置,这可能会给你一个满意的结果。

    data = mvnrnd([0, 0], eye(2), 10e4);
    
    %define the x and y axis
    edges{1} = -4:0.5:4;
    edges{2} = -4:0.2:4;
    
    hist3(data, 'Edges', edges);
    
    [N,C]  = hist3(data, 'Edges', edges);
    
    x_diff = diff(edges{1});
    y_diff = diff(edges{2});
    x = repmat([x_diff, x_diff(end)], length(edges{2}),1)';
    y = repmat([y_diff, y_diff(end)], length(edges{1}),1);
    
    % volume of the histogram
    V_tot  = sum(sum(x.*y.*N));
    
    N_norm = N/V_tot;
    
    figure
    % plot normalized histogram
    bar3(-C{2}, N_norm');
    axis normal
    

    请注意,bar3 图需要一些后处理:更改轴的刻度,可能还有条形之间的间隙和颜色。 我无法发布图片,所以您应该尝试运行代码并检查结果是否可以接受。

    编辑:或者您可以使用V_tot修改直方图z轴上的刻度标签。

    编辑:更改 z 轴的刻度标签(无 bar3 图):

    data = mvnrnd([0, 0], eye(2), 10e4);
    
    %define the x and y axis
    edges{1} = -4:0.5:4;
    edges{2} = -4:0.2:4;
    
    hist3(data, 'Edges', edges);
    
    [N,C]  = hist3(data, 'Edges', edges);
    
    x_diff = diff(edges{1});
    y_diff = diff(edges{2});
    x = repmat([x_diff, x_diff(end)], length(edges{2}),1)';
    y = repmat([y_diff, y_diff(end)], length(edges{1}),1);
    
    % volume of the histogram
    V_tot  = sum(sum(x.*y.*N));
    
    % change the Z tick labels
    z_tval = get(gca, 'ZTick');
    z_norm_tval = z_tval/V_tot;
    set(gca, 'ZTickLabel', z_norm_tval)
    

    【讨论】:

    • 非常感谢。我已根据您的代码编辑了问题。如何使用 V_tot 改变抽动?
    • @KaMu 我扩展了关于如何更改刻度标签的答案。希望这可以帮助。我不清楚你是如何得到负值的,没有你的数据(of.mat)很难弄清楚。也许您可以将图片上传到外部网站并发布链接。
    • @KaMu 如果您认为我的回答解决了您的问题,请接受它,下次我也可以发布图片。谢谢。
    • 非常感谢,我还在调查为什么我会变得消极,我会尽快发布图片。同时,你能告诉我这个代码部分实际上是做什么的吗? x = repmat([x_diff, x_diff(end)], length(edges{2}),1)'; y = repmat([y_diff, y_diff(end)], length(edges{1}),1);
    • repmat 函数复制距离向量(x_diffy_diff)。这里的距离是指相邻刻度之间的距离;复制次数等于另一个(垂直)轴上的刻度数。基本上xy 矩阵分别包含每个箱子(矩形)每个 x 和 y 轴的尺寸,因此,x.*y 矩阵显示箱子(矩形)的面积。如果你有等间距的刻度 xy 的所有元素将是相同的。
    猜你喜欢
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多