Plot CDF or PDF to compare matrices
15 views (last 30 days)
Show older comments
Hello everyone,
I'm looking for some recomendation regarding the best way to plot my data.
I have several a matricis (size 307 x 176) that I want to compare. They represent a heatmap of a specific species distribution.
The thing is: comparing all maps through a figure is difficult, not precise and consequently not recomended.
For this reason, I was thinking to use a cdf or a pdf functions to compare all of those matrices. The problem is I don't know how to use these functions in matrices.
Thank you in advance
0 Comments
Answers (2)
Gourab
on 19 Jun 2023
Hi Ricardo,
I understand that you want to compare the heatmaps using cdf or pdf function.
The "histogram()" function in MATLAB is used to plot the PDF and "ecdf()" function in MATLAB is used to plot CDFs for each heatmap. The function computes the empirical cumulative distribution function (ECDF) for each heatmap.
You can refer to the below code snippet.
load heatmaps.mat;
% Plot PDFs for each heatmap
figure;
hold on;
colors = {'r', 'g', 'b', 'm', 'c'};
for i = 1:numel(heatmaps)
h = histogram(heatmaps{i}(:), 'Normalization', 'pdf', 'FaceColor', colors{i}, 'EdgeColor', 'none');
end
legend({'Heatmap 1', 'Heatmap 2', 'Heatmap 3', 'Heatmap 4', 'Heatmap 5'}, 'Location', 'northeast');
xlabel('Value');
ylabel('Probability Density');
title('Heatmap PDF Comparison');
figure;
hold on;
for i = 1:numel(heatmaps)
[f, x] = ecdf(heatmaps{i}(:));
plot(x, f, colors{i}, 'LineWidth', 2);
end
legend({'Heatmap 1', 'Heatmap 2', 'Heatmap 3', 'Heatmap 4', 'Heatmap 5'}, 'Location', 'southeast');
xlabel('Value');
ylabel('Cumulative Probability');
title('Heatmap CDF Comparison');
You can refer to the below documentation link for more information on "histogram()" and "ecdf()" function.
3 Comments
Gourab
on 20 Jun 2023
Hello Ricardo
Here heatmaps is used as a cell array.Can you add the following lines to make sure heatmaps is a cell array.
heatmaps_data = load('heatmaps.mat');
heatmaps = struct2cell(heatmaps_data);
Sharad
on 19 Jun 2023
Hi,
In order to compare the matrices with the help of CDF and PDF functions in MATLAB, you can follow these steps.
- Install the Statistics and Machine Learning Toolbox from the MATLAB add on explorer. This toolbox is necessary for using these functions.
- Calculate the CDF and PDF for each of the matrices, using cdf and pdf functions. Here is an example:
% Taking two example matrices
h1 = rand(307, 176);
h2 = rand(307,176);
% Reshape the matrix into a vector
vector1 = reshape(h1, [], 1);
vector2 = reshape(h2, [], 1);
% Calculate the cdf and pdf
cdf1 = cdf('norm', vector1, mean(vector1), std(vector1)); % Assuming a normal distribution
pdf1 = pdf('norm', vector1, mean(vector1), std(vector1));
cdf2 = cdf('norm', vector2, mean(vector2), std(vector2));
pdf2 = pdf('norm', vector2, mean(vector2), std(vector2));
- Analyze the two distributions using tests like the Kolmogorov-Smirnov test and metrics like Kullback-Leibler (KL) divergence.
% Perform Kolmogorov-Smirnov test
[h, p] = kstest2(cdf1, cdf2);
if h
disp('The CDFs are significantly different.');
else
disp('The CDFs are not significantly different.');
end
% Calculate KL divergence
kl_divergence = sum(pdf1 .* log(pdf1 ./ pdf2));
disp(['KL Divergence: ', num2str(kl_divergence)]);
0 Comments
See Also
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!