Fitting 2D Gaussian to histogram

6 views (last 30 days)
Jose Aroca
Jose Aroca on 12 Aug 2020
Answered: J. Alex Lee on 12 Aug 2020
I have the following code that produces a 2D histogram from set of values A and B.
clearvars;
A = [574.960860000000;611.485460000000;524.571760000000;514.842210000000;598.114970000000;491.960270000000;508.391900000000;616.155440000000;593.879650000000;754.432240000000;486.983910000000;527.445680000000;703.829990000000;656.286210000000;607.536290000000;652.869050000000;645.273860000000;639.097820000000;510.208630000000;678.420820000000;638.812210000000;516.686170000000;509.171800000000;483.992230000000;568.424620000000;568.230780000000;499.795000000000;531.383150000000;666.895130000000;535.477650000000;559.002480000000;519.970530000000;509.720660000000;618.350170000000;1286.88303000000;490.833310000000;619.505590000000;577.211400000000;545.496580000000;593.934650000000;582.474160000000;675.700890000000;651.194190000000;608.515580000000;662.428050000000;604.179320000000;566.825610000000;664.431500000000;633.833680000000;548.563330000000;654.042420000000;603.685390000000;655.107950000000;637.332220000000;696.687700000000;600.853510000000;495.707310000000;603.626640000000;565.603220000000;636.245990000000;579.059610000000;573.181070000000;531.774700000000;702.800550000000;535.953710000000;634.029890000000;572.314750000000;481.509920000000;742.001560000000;612.678920000000;531.988950000000;534.944980000000;543.984030000000;602.732060000000;597.115220000000;655.685010000000;622.859220000000;492.035320000000;636.199310000000;637.870790000000;576.950090000000;658.162050000000;618.287800000000;529.712930000000;633.547230000000;632.580690000000;603.836420000000;596.802690000000;535.022630000000;629.326970000000;554.693700000000;446.187050000000;489.830910000000;524.309630000000;582.848040000000;559.817280000000;604.198790000000;562.603090000000;550.835390000000;598.106590000000;547.133510000000;472.302160000000;561.232810000000;729.946130000000;585.543430000000;626.338720000000;686.171910000000;584.553010000000;575.517990000000;473.594310000000;617.660830000000;545.610790000000;508.386700000000;466.928410000000;526.655980000000;599.911130000000;581.644600000000;587.530090000000;674.083760000000;613.411210000000;612.020620000000;502.766050000000;670.755630000000;713.258540000000;498.133580000000;663.246370000000;534.888720000000;574.138150000000;612.269290000000;562.863680000000;659.079680000000;542.592900000000;563.341270000000;592.884550000000;504.561700000000;411.632110000000;512.670970000000;940.790360000000;1277.16612000000;534.878580000000;497.024820000000;630.440170000000;512.925720000000;479.407680000000;615.624140000000;583.499710000000;553.221960000000;594.585360000000;447.051220000000;638.549770000000;573.649520000000;1154.09029000000;538.742090000000;580.763720000000;599.241840000000;450.564620000000;560.764910000000;526.070110000000;585.507000000000;677.124850000000;543.876390000000;550.833710000000;612.229020000000;670.676600000000;521.250600000000;586.459890000000;632.046090000000;655.352410000000;552.543780000000;562.665230000000;535.344280000000;584.146800000000;536.664670000000;582.770740000000;565.303550000000;531.783430000000;595.300600000000;517.277780000000;554.306190000000;507.307400000000;608.292620000000;610.784270000000;522.197670000000;590.532110000000;505.798900000000;694.155400000000;494.992080000000;541.604190000000;674.300750000000;524.013130000000;514.622080000000;704.525480000000;500.277540000000;653.403990000000;614.892320000000;616.564710000000;607.918330000000;593.632770000000;581.455910000000;578.675540000000;669.692640000000;654.155180000000;432.331150000000;579.045670000000;575.667250000000;577.207310000000;667.814550000000;474.865250000000;493.718020000000;590.688540000000;665.611970000000;1212.39650000000;637.844100000000;659.319070000000;574.044350000000;712.260590000000;476.533730000000;528.857660000000;467.386030000000;553.112580000000;609.878710000000;568.560230000000;578.258590000000;653.887450000000;506.013400000000;625.688710000000;644.518460000000;578.267020000000;495.207500000000;546.574370000000;731.636770000000;597.692800000000;603.617350000000;551.593630000000;539.999530000000;627.783440000000;690.491230000000;447.771970000000;598.423230000000;517.538260000000;495.669550000000;505.057590000000;526.452210000000;501.426590000000;495.028350000000;433.536110000000;1368.79567000000;514.689720000000;585.288860000000;580.674320000000;486.475920000000;550.459030000000;531.324260000000;608.041820000000;655.314640000000;616.131780000000;593.112740000000;676.421670000000;514.356380000000;586.535170000000;576.789960000000;582.722520000000;555.403500000000;468.318170000000;618.257650000000;595.145100000000;565.563520000000;584.240180000000;452.927410000000;587.342370000000;576.073850000000;425.410400000000;614.709250000000;687.720710000000;596.966670000000;564.673210000000;609.309060000000;533.215740000000;620.847220000000;503.513380000000;538.884960000000;554.625680000000;562.650840000000;605.715940000000;587.991540000000;558.775110000000;583.482280000000;527.563530000000;611.986550000000;618.767510000000;512.528490000000;546.838990000000;587.798680000000;504.451560000000;483.154050000000;493.075410000000;631.225710000000;556.694300000000;456.450040000000;578.934630000000;603.108560000000;600.955440000000;641.881700000000;589.967360000000;621.807460000000;571.037320000000;627.354220000000;567.872470000000;641.332380000000;729.461130000000;550.798860000000;610.769560000000;658.874900000000;623.275680000000;466.314450000000;659.392710000000;607.050440000000;738.370590000000;555.996330000000;675.381350000000;558.136940000000;604.256200000000;572.937590000000;638.779310000000;626.899840000000;658.353120000000;602.218100000000;638.770200000000;639.872730000000;568.067920000000;464.076450000000;644.435850000000;487.362620000000;479.582330000000;641.447080000000;588.327930000000;1138.30889000000;551.779080000000;533.237990000000;568.408890000000;728.524470000000;589.876200000000;522.023820000000;661.265320000000;593.848110000000;625.248600000000;578.545080000000;511.413950000000;593.718520000000;583.671750000000;608.052290000000;608.085080000000;626.915140000000;410.154260000000;503.335540000000;558.149350000000;649.460740000000;545.380770000000;550.623710000000;716.920920000000;572.624470000000;617.430690000000;665.077340000000;462.739000000000;542.221800000000;576.670940000000;580.786100000000;731.440360000000;625.441370000000;512.963570000000;609.212490000000;664.079540000000;539.691330000000;552.786490000000;550.986990000000;447.855710000000;559.841010000000;497.034490000000;446.657250000000;623.574930000000;632.885980000000;640.670090000000;505.432090000000;622.902590000000;666.712030000000;551.603220000000;604.120170000000;555.413150000000;509.007330000000;557.427610000000;551.563100000000;694.260360000000;628.657680000000;577.847250000000;648.978650000000;610.622580000000;457.913510000000;617.094620000000;502.014140000000;493.547500000000;577.284920000000;535.512070000000;541.346050000000;637.298260000000;528.040940000000;778.737380000000;579.806880000000;628.611410000000;595.967840000000;532.850190000000;491.231100000000;604.595730000000;473.862460000000;539.953280000000;453.469850000000;598.184880000000;528.170240000000;488.957210000000;628.951310000000;616.819930000000;524.291700000000;503.621420000000;490.044940000000;535.896540000000;587.500650000000;594.175250000000;591.187450000000;649.406110000000;515.546090000000;565.633380000000;580.653400000000;654.386930000000;640.276770000000;567.823000000000;487.725800000000;598.945150000000;447.161890000000;585.295430000000;538.395160000000;728.993430000000;619.280850000000;542.821590000000;503.419700000000;610.663260000000;582.975440000000;536.741620000000;710.264170000000;601.773600000000;425.503520000000;607.094410000000;593.164720000000;476.249730000000;765.677240000000;653.237310000000;561.489070000000;588.011060000000;629.360700000000;691.782430000000;530.465690000000;601.600540000000;664.749120000000;456.744390000000;474.734740000000;567.399750000000;522.035060000000;594.621130000000;508.877320000000;333.270620000000;580.204420000000;633.164120000000;563.666240000000;585.624260000000;579.917870000000;570.757160000000;580.458670000000;566.920980000000;493.595080000000;563.550810000000;616.326930000000;669.037710000000;560.871940000000;706.179560000000;696.516050000000;624.186300000000;596.794280000000;707.990090000000;633.535640000000;487.894020000000;605.913860000000;569.969060000000];
B = [385.926510000000;474.307760000000;367.976920000000;351.247700000000;512.414210000000;473.975260000000;410.121490000000;460.687420000000;421.967050000000;483.568780000000;459.164270000000;327.391160000000;422.793780000000;398.426670000000;490.634810000000;512.843870000000;483.309490000000;376.356450000000;389.687250000000;643.947470000000;439.332880000000;488.748840000000;537.583470000000;545.139320000000;503.096090000000;374.640460000000;377.887320000000;446.561530000000;442.839640000000;390.099370000000;490.451230000000;475.200200000000;452.741870000000;440.477300000000;785.496530000000;393.303360000000;465.337470000000;449.277600000000;497.174600000000;434.319100000000;462.900530000000;483.797060000000;408.756170000000;468.576840000000;555.520070000000;371.181310000000;479.053430000000;414.885370000000;462.099990000000;435.309740000000;432.431570000000;478.124740000000;521.725380000000;391.719710000000;413.861430000000;491.237730000000;435.255670000000;497.125980000000;377.089830000000;523.316150000000;464.530420000000;524.518780000000;486.218580000000;449.011560000000;529.177780000000;562.289660000000;352.829560000000;585.211040000000;430.138220000000;471.349690000000;439.605830000000;505.017080000000;474.803290000000;399.534380000000;434.579680000000;450.255730000000;457.688960000000;416.746820000000;386.594130000000;494.728820000000;452.620860000000;469.668090000000;362.198830000000;487.480220000000;423.686720000000;394.451340000000;494.113980000000;518.228710000000;523.014600000000;388.856790000000;506.101950000000;405.947330000000;433.245120000000;474.779440000000;410.719110000000;542.728420000000;483.873370000000;485.634570000000;571.917840000000;426.618410000000;449.990210000000;379.281920000000;393.251520000000;329.549020000000;481.831790000000;403.473570000000;426.081070000000;402.474860000000;512.961430000000;341.770470000000;453.994590000000;346.148770000000;395.111760000000;547.045590000000;452.431370000000;440.739090000000;465.414090000000;492.662540000000;456.583770000000;371.782000000000;426.617590000000;398.337580000000;403.771500000000;350.480140000000;661.461880000000;414.032470000000;403.025260000000;473.482990000000;433.159830000000;437.695910000000;442.310580000000;603.304350000000;453.636470000000;520.187040000000;522.475600000000;483.636050000000;441.138520000000;1802.80583000000;1475.77660000000;405.393330000000;525.572130000000;505.820660000000;354.087080000000;325.679410000000;508.683330000000;532.865820000000;462.251250000000;449.525320000000;516.613140000000;478.920020000000;433.844170000000;995.125550000000;403.227530000000;406.690320000000;396.269080000000;468.454750000000;487.133540000000;406.924640000000;440.840120000000;593.481400000000;445.219290000000;383.930290000000;439.546300000000;745.878030000000;380.063020000000;485.530660000000;516.459610000000;479.629640000000;347.884930000000;352.420140000000;418.826710000000;351.997780000000;452.976320000000;437.517220000000;463.074910000000;390.158100000000;455.959440000000;492.938120000000;503.707120000000;324.473260000000;307.756870000000;465.342040000000;428.020740000000;410.824920000000;365.359590000000;383.600850000000;456.536390000000;388.468110000000;377.578300000000;382.861930000000;339.848300000000;537.251790000000;475.080250000000;385.965950000000;437.639910000000;495.200040000000;451.206580000000;408.316070000000;487.388960000000;312.745510000000;391.478880000000;385.671660000000;465.382180000000;435.064030000000;480.513260000000;522.436590000000;506.988120000000;364.386450000000;428.195650000000;558.093320000000;385.902540000000;1158.25652000000;425.368430000000;438.465500000000;432.927630000000;376.923880000000;315.187410000000;525.877520000000;431.342250000000;494.996140000000;504.165900000000;539.370100000000;438.954440000000;509.492080000000;508.939710000000;490.227670000000;620.487660000000;498.638640000000;421.273550000000;527.136980000000;376.168240000000;479.316450000000;409.405500000000;336.919500000000;441.629190000000;573.701660000000;456.272970000000;568.015270000000;471.377570000000;422.346930000000;420.493000000000;446.400930000000;529.751310000000;546.136610000000;475.067720000000;439.114350000000;1388.49481000000;410.167250000000;353.546130000000;411.103830000000;469.324460000000;523.577100000000;465.160570000000;483.748420000000;553.713090000000;496.191410000000;436.756050000000;412.747260000000;370.509860000000;430.273670000000;433.282960000000;405.798870000000;445.380980000000;410.786140000000;592.505220000000;483.735150000000;452.636790000000;421.772750000000;515.543920000000;536.344760000000;414.774720000000;397.225540000000;412.714150000000;467.792820000000;512.376890000000;386.024650000000;479.683340000000;418.031140000000;436.939740000000;502.871520000000;495.408510000000;457.468790000000;456.251630000000;402.885570000000;399.176820000000;498.519160000000;476.619990000000;492.675310000000;434.734390000000;368.202730000000;523.220520000000;558.204110000000;416.176650000000;458.933570000000;520.619380000000;559.180430000000;347.007970000000;366.672540000000;430.214610000000;552.870900000000;533.143350000000;472.652760000000;500.421150000000;399.384220000000;507.060340000000;522.262980000000;440.950770000000;448.722310000000;450.007640000000;464.189570000000;370.469340000000;390.136920000000;432.054460000000;441.084430000000;462.582470000000;566.844570000000;376.094000000000;447.343470000000;573.719740000000;375.551780000000;387.908350000000;303.777210000000;438.020870000000;545.805670000000;453.802810000000;371.536440000000;561.471770000000;426.550110000000;518.472640000000;439.392240000000;483.650640000000;405.162590000000;460.062590000000;471.901210000000;425.705590000000;487.996340000000;874.798480000000;475.323340000000;440.794780000000;349.581680000000;364.849140000000;462.003700000000;555.595810000000;400.346630000000;486.556240000000;460.302720000000;579.161220000000;447.897770000000;537.660230000000;490.245550000000;329.495800000000;500.858740000000;456.851760000000;246.005070000000;498.447550000000;472.888510000000;445.388410000000;507.680660000000;448.916160000000;419.906890000000;545.890230000000;386.460500000000;425.991460000000;447.007520000000;485.203850000000;506.246720000000;391.501620000000;484.064190000000;429.264360000000;535.752340000000;466.896570000000;566.156590000000;460.994630000000;370.240400000000;416.865000000000;383.847520000000;554.843580000000;518.182950000000;342.099300000000;459.628650000000;451.176230000000;433.837600000000;441.648060000000;483.796250000000;439.141010000000;454.282400000000;549.857350000000;414.169260000000;376.201710000000;441.645000000000;512.177140000000;399.431930000000;343.302630000000;482.503630000000;376.464810000000;442.585240000000;513.198140000000;521.153090000000;500.999780000000;492.682980000000;339.243120000000;440.456800000000;546.939640000000;445.248240000000;461.440750000000;447.717690000000;509.058450000000;348.085300000000;365.898400000000;400.116300000000;472.133220000000;585.342280000000;550.285830000000;498.197500000000;459.966000000000;416.635330000000;566.340650000000;507.750090000000;420.925470000000;404.309610000000;530.860030000000;498.770020000000;380.093520000000;394.772690000000;445.327240000000;512.218060000000;503.802980000000;450.579910000000;511.202360000000;479.260920000000;440.296530000000;505.763860000000;505.888740000000;692.241910000000;505.151180000000;525.890360000000;486.565460000000;402.635410000000;486.635510000000;487.765420000000;509.898100000000;462.305050000000;411.516830000000;529.873150000000;428.181230000000;470.946600000000;412.677030000000;420.051390000000;415.292940000000;531.751190000000;551.992840000000;395.468950000000;409.730690000000;492.705880000000;472.443930000000;482.162230000000;596.648130000000;381.357090000000;507.738320000000;425.684550000000;490.386110000000;476.348170000000;493.009390000000;488.131670000000;695.067480000000;474.366410000000;440.487660000000;552.999120000000;572.999060000000;455.645670000000;431.379550000000;506.555800000000;451.608180000000;431.204710000000;518.606960000000;450.578540000000;465.519830000000;513.974450000000;401.994960000000;425.333830000000;446.199550000000;498.291440000000;469.726080000000;497.989620000000;439.968830000000;445.624060000000;459.798260000000;384.031890000000;437.434840000000;500.940360000000];
histogram2(A,B,[50 50],'FaceColor','flat');
colorbar
xlim ([200 900])
ylim ([100 900])
I want to fit a 2D gaussian to the data so I can determine the centre of population, but I am unsure how to do so.

Answers (1)

J. Alex Lee
J. Alex Lee on 12 Aug 2020
If you only need the center, you can just take the mean in each direction
x = mean(A)
y = mean(B)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!