Plot CDF or PDF to compare matrices

14 views (last 30 days)
Ricardo Duarte
Ricardo Duarte on 18 Jun 2023
Commented: Ricardo Duarte on 20 Jun 2023
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

Answers (2)

Gourab
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
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);
Ricardo Duarte
Ricardo Duarte on 20 Jun 2023
Hello @Gourab
Probably I was not precise in my explaination.
In reality I don't have a heatmap. What I have is a matrix that I used to create a kind of heatmap using the surf function. Let's say I have longitude, latitude and distribution and I used that to create a plot similar to a heatmap.
Maybe because of that the error remains.
Thank you.

Sign in to comment.


Sharad
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)]);

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!