what kind of plot is useful for comparing two matrices?

15 views (last 30 days)
I have two 5*5 matrices. I want to use a plot to show that they are close element-wise.
What plot is suitable?
I think the plot should be transparent.
It is of course doable if we reshape the matrices into vectors, and use a 2d plot. But that is not intuitive.

Accepted Answer

Star Strider
Star Strider on 9 Jun 2024
I am not certain that there is any built-in functtion for this sort of problem.
One approach —
M1 = randn(5)
M1 = 5x5
1.0335 0.3537 0.1255 0.0657 0.1121 -1.0280 0.0115 -0.0957 -0.4548 -0.6092 0.1566 0.3970 1.2606 -0.1277 -0.1152 -1.2395 -0.4992 -0.2599 0.6389 -0.0945 -0.4119 -0.1545 0.6857 -3.2695 0.0717
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
M2 = M1 + randn(5)/10
M2 = 5x5
1.0987 0.3445 0.0749 -0.0346 0.0453 -0.9348 -0.1396 -0.2144 -0.2877 -0.6895 0.2739 0.4181 1.3805 -0.1310 -0.1254 -1.3055 -0.5989 -0.3736 0.6637 -0.0121 -0.3835 -0.3049 0.7109 -3.2123 0.2489
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Mcomp = sqrt((M1-M2).^2/numel(M1));
ms = fitdist(Mcomp(:),'lognormal')
ms =
LognormalDistribution Lognormal distribution mu = -4.47506 [-4.8949, -4.05523] sigma = 1.01709 [0.794171, 1.41492]
lognparms = @(mu,sigma) [exp(mu + sigma^2/2); exp(2*mu + sigma^2) * (exp(sigma^2)-1); sqrt(exp(2*mu + sigma^2) * (exp(sigma^2)-1))]; % [mean; var; std]
lnparms = lognparms(ms.mu, ms.sigma);
fprintf(1,'\nMean = %.6f\nVar = %.6f\nStDv = %.6f\n',lnparms)
Mean = 0.019105 Var = 0.000662 StDv = 0.025728
[X,Y] = meshgrid(1:5);
figure
stem3(Mcomp)
hold on
scatter3(X(:), Y(:), Mcomp(:), 50, Mcomp(:), 'filled')
patch([xlim flip(xlim)], [1 1 5 5], ones(1,4)*lnparms(1), 'k', 'FaceAlpha',0.25, 'EdgeColor','none')
hold off
colormap(turbo)
colorbar
axis('padded')
zlabel(["‘Mcomp’ Values For Each" "Comparison Matrix Element"])
The comparison value ‘Mcomp’ is a ‘sort of’ root-mean-square value. (It can be anything, providing it returns a matrix equal to the original matrices.) Since the ‘Mcomp’ calculation I chose can never be negative (and would likely be strictly positive, so non-zero as well), I chose the lognormal distribution to calculate its parameters. If you choose a different calculation, choose an appropriate distribution if you want to calculate its parameters. The gray patch is the mean of the ‘Mcomp’ values, as calculated from the lognormal distribution parameters.
.
  5 Comments
min lee
min lee on 10 Jun 2024
Ah, I think this is what I want! Thank you very much!

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 9 Jun 2024
Edited: Image Analyst on 10 Jun 2024
Why a plot and not a 2-D image showing differences? What would your x axis represent?
If you treat your matrix as an image there are lots of ways to compare images. For example imabsdiff, immse, psnr, etc. For example you could use imabsdiff and color code the magnitude of the differences in an image, or you could count the number of matches and mismatches. You could also make a histogram of the differences - that's a plot.
immse and psnr give single numbers gauging how close the matrices are to each other.
Here's some code
% Create two matrices
m1 = uint8(randi(255, 5, 5))
m1 = 5x5
2 151 230 193 31 188 128 17 133 172 84 164 69 28 168 30 162 145 124 116 190 220 77 212 194
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
m2 = uint8(randi(255, 5, 5))
m2 = 5x5
97 14 248 53 201 225 50 103 219 151 12 24 155 251 182 57 240 80 181 76 254 201 58 19 92
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Compute difference.
diffMatrix = imabsdiff(m1, m2)
diffMatrix = 5x5
95 137 18 140 170 37 78 86 86 21 72 140 86 223 14 27 78 65 57 40 64 19 19 193 102
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Display m1
subplot(2, 2, 1);
imshow(m1)
title('m1');
% Display m2
subplot(2, 2, 2);
imshow(m2)
title('m2');
% Display diffMatrix
p3 = subplot(2, 2, 3);
imshow(diffMatrix)
title('diffMatrix');
colorbar(p3)
colormap(p3, 'turbo')

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!