gaussian fit to 2D pcolor map

4 views (last 30 days)
Ham Man
Ham Man on 19 Aug 2022
Edited: Matt J on 27 Aug 2022
I have a 2D map using pcolor function. Now I want to fit gaussian to this 2D map P1. How can I do that?
%[xx,yy] = meshgrid(x,y);
xx = [ 3.9202 7.6226 11.3250 15.0274 18.7298 22.4323
3.9202 7.6226 11.3250 15.0274 18.7298 22.4323
3.9202 7.6226 11.3250 15.0274 18.7298 22.4323
3.9202 7.6226 11.3250 15.0274 18.7298 22.4323
3.9202 7.6226 11.3250 15.0274 18.7298 22.4323
3.9202 7.6226 11.3250 15.0274 18.7298 22.4323
3.9202 7.6226 11.3250 15.0274 18.7298 22.4323
3.9202 7.6226 11.3250 15.0274 18.7298 22.4323];% 8x6 matrix
yy = [ 5.0820 5.0820 5.0820 5.0820 5.0820 5.0820
6.6220 6.6220 6.6220 6.6220 6.6220 6.6220
8.1620 8.1620 8.1620 8.1620 8.1620 8.1620
9.7020 9.7020 9.7020 9.7020 9.7020 9.7020
11.2420 11.2420 11.2420 11.2420 11.2420 11.2420
12.7820 12.7820 12.7820 12.7820 12.7820 12.7820
14.3220 14.3220 14.3220 14.3220 14.3220 14.3220
15.8620 15.8620 15.8620 15.8620 15.8620 15.8620];% 8x6 matrix
C = [ 0 30 133 199 143 27 0 NaN
123 614 765 832 810 590 100 NaN
388 787 897 891 903 857 442 NaN
125 570 744 737 782 659 176 NaN
0 53 180 270 210 63 0 NaN
NaN NaN NaN NaN NaN NaN NaN NaN] ;% C is a 6x8 matrix of data
P1 = pcolor(xx',yy',C);

Answers (1)

Matt J
Matt J on 19 Aug 2022
Edited: Matt J on 19 Aug 2022
You could try gaussfitn,
idx=~isnan(C);
xy=[xx(idx),yy(idx)];
params=gaussfitn(xy,C(idx));
[D,A,mu,sigma]=deal(params{:})
D =
190.8900
A =
835.3517
mu =
11.3936
11.1766
sigma =
151.2489 -130.1645
-130.1645 114.3164
  8 Comments
Matt J
Matt J on 27 Aug 2022
Edited: Matt J on 27 Aug 2022
Perhaps as follows.
idx = ~isnan(C);
xy = [xx(idx),yy(idx)];
params = gaussfitn(xy,C(idx));
[D,A,mu,sigma] = deal(params{:});
% asuume mu=0,bucause mu from param is 2x1 matrix and the dim is
% not agree for z calculation:
[X,Y]=meshgrid(mu(1)+linspace(-3,3)*sqrt(sigma(1)),...
mu(2)+linspace(-3,3)*sqrt(sigma(end)),...);
dXY=[X(:),Y(:)]-mu(:)';
Z = D + A*exp( -0.5 *sum((sigma\dXY).*dXY.',1) );
Z=reshape(Z,size(X));
surf(X,Y,Z); hold on;
scatter(xy(:,1),xy(:,2)); hold off

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!