- Accumulate Histogram Counts: Use the histcounts function to compute histogram counts for each dataset and accumulate these counts. The processDirectory function reads images, computes histogram counts for each, and adds them to a cumulative count.
- Plot the Combined Histogram: Use the histogram function to plot the accumulated histogram counts.
How to sum different histograms in just one?
1 view (last 30 days)
Show older comments
I have tried with the bars because I could not do it with the histogram. How can I do the same with the histogram function?
clc
clear all
close all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Tr=25 years %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
File50=dir('C:\Users\Daniela\OneDrive - Politecnico di Milano\THESIS\Data process\Statistics\Harzard\Ourthe\tr_25');
for k=3:length(File50)
FileNames25=File50(k).name;
end
i25=0;
N_new25=0;
a=0.001;
b=5;
h=0.1;
for k=3:length(File50)
i=i+1;
m_i25=File50(3).name;
A_i25=imread(m_i25);
image(A_i25);
max_depth25=max(A_i25,[],'all');
min_depth25=min(A_i25 (A_i25>0));
I_i25=find(A_i25>0.001);
x=[a:h:b];
y=[a:h:b-0.1];
[N_i]=histcounts(A_i25,x);
N_old25=N_new25;
N_new25=N_old25+N_i;
end
area_25=sum(N_new25)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Tr=50 years %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
File50=dir('C:\Users\Daniela\OneDrive - Politecnico di Milano\THESIS\Data process\Statistics\Harzard\Ourthe\tr_50');
for k=1:length(File50)
FileNames50=File50(k).name;
end
i=0;
N_new50=0;
for k=1:length(File50)
i=i+1;
m_i50=File50(3).name;
A_i50=imread(m_i50);
image(A_i50);
max_depth50=max(A_i50,[],'all');
min_depth50=min(A_i50 (A_i50>0));
I_i50=find(A_i50>0.001);
x=[a:h:b];
y=[a:h:b-0.1];
[N_i]=histcounts(A_i50,x);
N_old50=N_new50;
N_new50=N_old50+N_i;
end
area_50=sum(N_new50)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Tr=100 years %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
File100=dir('C:\Users\Daniela\OneDrive - Politecnico di Milano\THESIS\Data process\Statistics\Harzard\Ourthe\tr_100');
for k=1:length(File100)
FileNames100=File100(k).name;
end
i=0;
N_new100=0;
for k=1:length(File100)
i=i+1;
m_i100=File100(3).name;
A_i100=imread(m_i100);
image(A_i100);
max_depth100=max(A_i100,[],'all');
min_depth100=min(A_i100 (A_i100>0));
I_i100=find(A_i100>0.001);
x=[a:h:b];
y=[a:h:b-0.1];
[N_i]=histcounts(A_i100,x);
N_old100=N_new100;
N_new100=N_old100+N_i;
end
area_100=sum(N_new100)
figure (1)
bar(y,N_new100,'FaceAlpha',0.5,'FaceColor','y')
hold on
bar(y,N_new50,'FaceAlpha',0.5,'FaceColor',[1, 0.5, 0])
hold on
bar(y,N_new25,'FaceAlpha',0.5,'FaceColor','r')
title('Flooded area water depth - River Ourthe)');
xlabel('Water depth')
legend('tr=100 years','tr=50 years','tr=25 years')
0 Comments
Answers (1)
AKennedy
on 13 Feb 2025
% Define bin edges
% Initialize cumulative histogram counts
N_cumulative = zeros(1, length(x) - 1);
% Function to process each directory
function N_cumulative = processDirectory(directoryPath, x, N_cumulative)
files = dir(directoryPath);
for k = 3:length(files)
% Read each image
fileName = fullfile(directoryPath, files(k).name);
A = imread(fileName);
% Compute histogram counts
N_i = histcounts(A(A > 0.001), x);
% Accumulate counts
N_cumulative = N_cumulative + N_i;
end
end
% Process each directory
N_cumulative = processDirectory('C:\Users\Daniela\OneDrive - Politecnico di Milano\THESIS\Data process\Statistics\Harzard\Ourthe\tr_25', x, N_cumulative);
N_cumulative = processDirectory('C:\Users\Daniela\OneDrive - Politecnico di Milano\THESIS\Data process\Statistics\Harzard\Ourthe\tr_50', x, N_cumulative);
N_cumulative = processDirectory('C:\Users\Daniela\OneDrive - Politecnico di Milano\THESIS\Data process\Statistics\Harzard\Ourthe\tr_100', x, N_cumulative);
% Plot the cumulative histogram
figure;
histogram('BinEdges', x, 'BinCounts', N_cumulative, 'FaceAlpha', 0.5, 'FaceColor', 'b');
title('Cumulative Histogram of Flooded Area Water Depth - River Ourthe');
xlabel('Water Depth');
ylabel('Frequency');
0 Comments
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!