Fitting 2D Gaussian to histogram

13 views (last 30 days)
Jose Aroca
Jose Aroca on 12 Aug 2020
Edited: J. Alex Lee on 8 Aug 2025 at 11:16
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 (2)

J. Alex Lee
J. Alex Lee on 12 Aug 2020
Edited: J. Alex Lee on 8 Aug 2025 at 11:16
I updated the answer to show that means of each is sufficient to find the mean.
If you only need the center, you can just take the mean in each direction.
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];
mA = mean(A);
mB = mean(B);
histogram2(A,B,[50,50],'FaceColor','flat','normalization','pdf');
view(2)
colorbar
xlim ([200 900])
ylim ([100 900])
hold on
plot3(mA,mB,1,"*r")
xline(mA,'-r')
yline(mB,'-r')

Meg Noah
Meg Noah on 8 Aug 2025 at 1:27
Perhaps this way:
clearvars;
A = [574.96086;611.48546;524.57176;514.84221;598.11497;491.96027;508.39190;616.15544;593.87965;754.43224;486.98391;527.44568;703.82999;656.28621;607.53629;652.86905;645.27386;639.09782;510.20863;678.42082;638.81221;516.68617;509.17180;483.99223;568.42462;568.23078;499.79500;531.38315;666.89513;535.47765;559.00248;519.97053;509.72066;618.35017;1286.88303000000;490.83331;619.50559;577.21140;545.49658;593.93465;582.47416;675.70089;651.19419;608.51558;662.42805;604.17932;566.82561;664.43150;633.83368;548.56333;654.04242;603.68539;655.10795;637.33222;696.68770;600.85351;495.70731;603.62664;565.60322;636.24599;579.05961;573.18107;531.77470;702.80055;535.95371;634.02989;572.31475;481.50992;742.00156;612.67892;531.98895;534.94498;543.98403;602.73206;597.11522;655.68501;622.85922;492.03532;636.19931;637.87079;576.95009;658.16205;618.28780;529.71293;633.54723;632.58069;603.83642;596.80269;535.02263;629.32697;554.69370;446.18705;489.83091;524.30963;582.84804;559.81728;604.19879;562.60309;550.83539;598.10659;547.13351;472.30216;561.23281;729.94613;585.54343;626.33872;686.17191;584.55301;575.51799;473.59431;617.66083;545.61079;508.38670;466.92841;526.65598;599.91113;581.64460;587.53009;674.08376;613.41121;612.02062;502.76605;670.75563;713.25854;498.13358;663.24637;534.88872;574.13815;612.26929;562.86368;659.07968;542.59290;563.34127;592.88455;504.56170;411.63211;512.67097;940.79036;1277.16612000000;534.87858;497.02482;630.44017;512.92572;479.40768;615.62414;583.49971;553.22196;594.58536;447.05122;638.54977;573.64952;1154.09029000000;538.74209;580.76372;599.24184;450.56462;560.76491;526.07011;585.50700;677.12485;543.87639;550.83371;612.22902;670.67660;521.25060;586.45989;632.04609;655.35241;552.54378;562.66523;535.34428;584.14680;536.66467;582.77074;565.30355;531.78343;595.30060;517.27778;554.30619;507.30740;608.29262;610.78427;522.19767;590.53211;505.79890;694.15540;494.99208;541.60419;674.30075;524.01313;514.62208;704.52548;500.27754;653.40399;614.89232;616.56471;607.91833;593.63277;581.45591;578.67554;669.69264;654.15518;432.33115;579.04567;575.66725;577.20731;667.81455;474.86525;493.71802;590.68854;665.61197;1212.3965;637.84410;659.31907;574.04435;712.26059;476.53373;528.85766;467.38603;553.11258;609.87871;568.56023;578.25859;653.88745;506.01340;625.68871;644.51846;578.26702;495.20750;546.57437;731.63677;597.69280;603.61735;551.59363;539.99953;627.78344;690.49123;447.77197;598.42323;517.53826;495.66955;505.05759;526.45221;501.42659;495.02835;433.53611;1368.79567000000;514.68972;585.28886;580.67432;486.47592;550.45903;531.32426;608.04182;655.31464;616.13178;593.11274;676.42167;514.35638;586.53517;576.78996;582.72252;555.40350;468.31817;618.25765;595.14510;565.56352;584.24018;452.92741;587.34237;576.07385;425.41040;614.70925;687.72071;596.96667;564.67321;609.30906;533.21574;620.84722;503.51338;538.88496;554.62568;562.65084;605.71594;587.99154;558.77511;583.48228;527.56353;611.98655;618.76751;512.52849;546.83899;587.79868;504.45156;483.15405;493.07541;631.22571;556.69430;456.45004;578.93463;603.10856;600.95544;641.88170;589.96736;621.80746;571.03732;627.35422;567.87247;641.33238;729.46113;550.79886;610.76956;658.87490;623.27568;466.31445;659.39271;607.05044;738.37059;555.99633;675.38135;558.13694;604.25620;572.93759;638.77931;626.89984;658.35312;602.21810;638.77020;639.87273;568.06792;464.07645;644.43585;487.36262;479.58233;641.44708;588.32793;1138.30889000000;551.77908;533.23799;568.40889;728.52447;589.87620;522.02382;661.26532;593.84811;625.24860;578.54508;511.41395;593.71852;583.67175;608.05229;608.08508;626.91514;410.15426;503.33554;558.14935;649.46074;545.38077;550.62371;716.92092;572.62447;617.43069;665.07734;462.73900;542.22180;576.67094;580.78610;731.44036;625.44137;512.96357;609.21249;664.07954;539.69133;552.78649;550.98699;447.85571;559.84101;497.03449;446.65725;623.57493;632.88598;640.67009;505.43209;622.90259;666.71203;551.60322;604.12017;555.41315;509.00733;557.42761;551.56310;694.26036;628.65768;577.84725;648.97865;610.62258;457.91351;617.09462;502.01414;493.54750;577.28492;535.51207;541.34605;637.29826;528.04094;778.73738;579.80688;628.61141;595.96784;532.85019;491.23110;604.59573;473.86246;539.95328;453.46985;598.18488;528.17024;488.95721;628.95131;616.81993;524.29170;503.62142;490.04494;535.89654;587.50065;594.17525;591.18745;649.40611;515.54609;565.63338;580.65340;654.38693;640.27677;567.82300;487.72580;598.94515;447.16189;585.29543;538.39516;728.99343;619.28085;542.82159;503.41970;610.66326;582.97544;536.74162;710.26417;601.77360;425.50352;607.09441;593.16472;476.24973;765.67724;653.23731;561.48907;588.01106;629.36070;691.78243;530.46569;601.60054;664.74912;456.74439;474.73474;567.39975;522.03506;594.62113;508.87732;333.27062;580.20442;633.16412;563.66624;585.62426;579.91787;570.75716;580.45867;566.92098;493.59508;563.55081;616.32693;669.03771;560.87194;706.17956;696.51605;624.18630;596.79428;707.99009;633.53564;487.89402;605.91386;569.96906];
B = [385.92651;474.30776;367.97692;351.24770;512.41421;473.97526;410.12149;460.68742;421.96705;483.56878;459.16427;327.39116;422.79378;398.42667;490.63481;512.84387;483.30949;376.35645;389.68725;643.94747;439.33288;488.74884;537.58347;545.13932;503.09609;374.64046;377.88732;446.56153;442.83964;390.09937;490.45123;475.20020;452.74187;440.47730;785.49653;393.30336;465.33747;449.27760;497.17460;434.31910;462.90053;483.79706;408.75617;468.57684;555.52007;371.18131;479.05343;414.88537;462.09999;435.30974;432.43157;478.12474;521.72538;391.71971;413.86143;491.23773;435.25567;497.12598;377.08983;523.31615;464.53042;524.51878;486.21858;449.01156;529.17778;562.28966;352.82956;585.21104;430.13822;471.34969;439.60583;505.01708;474.80329;399.53438;434.57968;450.25573;457.68896;416.74682;386.59413;494.72882;452.62086;469.66809;362.19883;487.48022;423.68672;394.45134;494.11398;518.22871;523.01460;388.85679;506.10195;405.94733;433.24512;474.77944;410.71911;542.72842;483.87337;485.63457;571.91784;426.61841;449.99021;379.28192;393.25152;329.54902;481.83179;403.47357;426.08107;402.47486;512.96143;341.77047;453.99459;346.14877;395.11176;547.04559;452.43137;440.73909;465.41409;492.66254;456.58377;371.78200;426.61759;398.33758;403.77150;350.48014;661.46188;414.03247;403.02526;473.48299;433.15983;437.69591;442.31058;603.30435;453.63647;520.18704;522.47560;483.63605;441.13852;1802.80583000000;1475.7766;405.39333;525.57213;505.82066;354.08708;325.67941;508.68333;532.86582;462.25125;449.52532;516.61314;478.92002;433.84417;995.12555;403.22753;406.69032;396.26908;468.45475;487.13354;406.92464;440.84012;593.48140;445.21929;383.93029;439.54630;745.87803;380.06302;485.53066;516.45961;479.62964;347.88493;352.42014;418.82671;351.99778;452.97632;437.51722;463.07491;390.15810;455.95944;492.93812;503.70712;324.47326;307.75687;465.34204;428.02074;410.82492;365.35959;383.60085;456.53639;388.46811;377.57830;382.86193;339.84830;537.25179;475.08025;385.96595;437.63991;495.20004;451.20658;408.31607;487.38896;312.74551;391.47888;385.67166;465.38218;435.06403;480.51326;522.43659;506.98812;364.38645;428.19565;558.09332;385.90254;1158.25652000000;425.36843;438.46550;432.92763;376.92388;315.18741;525.87752;431.34225;494.99614;504.16590;539.37010;438.95444;509.49208;508.93971;490.22767;620.48766;498.63864;421.27355;527.13698;376.16824;479.31645;409.40550;336.91950;441.62919;573.70166;456.27297;568.01527;471.37757;422.34693;420.49300;446.40093;529.75131;546.13661;475.06772;439.11435;1388.49481000000;410.16725;353.54613;411.10383;469.32446;523.57710;465.16057;483.74842;553.71309;496.19141;436.75605;412.74726;370.50986;430.27367;433.28296;405.79887;445.38098;410.78614;592.50522;483.73515;452.63679;421.77275;515.54392;536.34476;414.77472;397.22554;412.71415;467.79282;512.37689;386.02465;479.68334;418.03114;436.93974;502.87152;495.40851;457.46879;456.25163;402.88557;399.17682;498.51916;476.61999;492.67531;434.73439;368.20273;523.22052;558.20411;416.17665;458.93357;520.61938;559.18043;347.00797;366.67254;430.21461;552.87090;533.14335;472.65276;500.42115;399.38422;507.06034;522.26298;440.95077;448.72231;450.00764;464.18957;370.46934;390.13692;432.05446;441.08443;462.58247;566.84457;376.09400;447.34347;573.71974;375.55178;387.90835;303.77721;438.02087;545.80567;453.80281;371.53644;561.47177;426.55011;518.47264;439.39224;483.65064;405.16259;460.06259;471.90121;425.70559;487.99634;874.79848;475.32334;440.79478;349.58168;364.84914;462.00370;555.59581;400.34663;486.55624;460.30272;579.16122;447.89777;537.66023;490.24555;329.49580;500.85874;456.85176;246.00507;498.44755;472.88851;445.38841;507.68066;448.91616;419.90689;545.89023;386.46050;425.99146;447.00752;485.20385;506.24672;391.50162;484.06419;429.26436;535.75234;466.89657;566.15659;460.99463;370.24040;416.86500;383.84752;554.84358;518.18295;342.09930;459.62865;451.17623;433.83760;441.64806;483.79625;439.14101;454.28240;549.85735;414.16926;376.20171;441.64500;512.17714;399.43193;343.30263;482.50363;376.46481;442.58524;513.19814;521.15309;500.99978;492.68298;339.24312;440.45680;546.93964;445.24824;461.44075;447.71769;509.05845;348.08530;365.89840;400.11630;472.13322;585.34228;550.28583;498.19750;459.96600;416.63533;566.34065;507.75009;420.92547;404.30961;530.86003;498.77002;380.09352;394.77269;445.32724;512.21806;503.80298;450.57991;511.20236;479.26092;440.29653;505.76386;505.88874;692.24191;505.15118;525.89036;486.56546;402.63541;486.63551;487.76542;509.89810;462.30505;411.51683;529.87315;428.18123;470.94660;412.67703;420.05139;415.29294;531.75119;551.99284;395.46895;409.73069;492.70588;472.44393;482.16223;596.64813;381.35709;507.73832;425.68455;490.38611;476.34817;493.00939;488.13167;695.06748;474.36641;440.48766;552.99912;572.99906;455.64567;431.37955;506.55580;451.60818;431.20471;518.60696;450.57854;465.51983;513.97445;401.99496;425.33383;446.19955;498.29144;469.72608;497.98962;439.96883;445.62406;459.79826;384.03189;437.43484;500.94036];
hdata = histogram2(A,B,[50 50],'FaceColor','flat');
colorbar
xlim ([200 900])
ylim ([100 900])
xVals = (hdata.XBinEdges(2:end) + hdata.XBinEdges(1:end-1))/2.0;
yVals = (hdata.YBinEdges(2:end) + hdata.YBinEdges(1:end-1))/2.0;
[xVal2D,yVal2D] = meshgrid(xVals,yVals);
zVal2D = hdata.Values;
N = size(zVal2D,1);
% initial uess
binaryImage = true(size(zVal2D));
binaryImage(zVal2D < min(zVal2D(zVal2D > 0))) = false;
zDesc = regionprops(logical(binaryImage), zVal2D, 'PixelIdxList', ...
'WeightedCentroid','Orientation','Area');
idx = find([zDesc.Area] == max([zDesc.Area]));
estC = zDesc(idx).WeightedCentroid;
estTheta = zDesc(idx).Orientation;
blob = zeros(size(zVal2D));
blob(zDesc(idx).PixelIdxList) = 1;
initParm = [1.2*max(zVal2D(:)) std(A)/2 std(B)/2 estTheta yVals(round(estC(1))) xVals(round(estC(2)))];
zVal2DCleaned = zVal2D;
zVal2DCleaned(blob==0) = 0;
[finalParm] = fitGauss2D(xVal2D,yVal2D,zVal2DCleaned,initParm);
Amp = finalParm(1);
xG = finalParm(2).^2;
yG = finalParm(3).^2;
theta = finalParm(4);
x0 = finalParm(5);
y0 = finalParm(6);
xC = ((xVal2D-x0).*cosd(theta) - (yVal2D-y0).*sind(theta)).^2;
yC = ((xVal2D-x0).*sind(theta) + (yVal2D-y0).*cosd(theta)).^2;
zModel = Amp.*exp( -(xC./(2.0*xG)) - (yC./(2.0*yG)) );
figure();
sgtitle('2D Gaussian Fit');
subplot(4,4,4)
histogram2(A,B,[50 50],'FaceColor','flat')
subplot(4,4,[5 6 7 9 10 11 13 14 15]);
imagesc(xVal2D(1,:),yVal2D(:,1)',zVal2D);
hold on;
set(gca,'ydir','normal');
col0 = find(xVals>x0,1,'first');
row0 = find(yVals>y0,1,'first');
myXLim = xlim;
myYLim = ylim;
plot([x0 x0],myYLim,'r');
plot(myXLim,[y0 y0],'k');
subplot(4,4,1:3);
plot(xVals,interp2(xVals,yVals,zVal2D,xVals,y0*ones(size(yVals))),'k.');
hold on;
plot(xVals,interp2(xVals,yVals,zModel,xVals,y0*ones(size(yVals))),'b.');
plot([x0 x0],[0 2*Amp],'r');
xlim(myXLim);
ylim([0 2*Amp]);
subplot(4,4,[8 12 16]);
plot(interp2(xVals,yVals,zVal2D,x0*ones(size(yVals)),yVals),yVals,'r.');
hold on;
plot(interp2(xVals,yVals,zModel,x0*ones(size(yVals)),yVals),yVals,'b.');
plot([0 2*Amp],[y0 y0],'k');
ylim(myYLim);
xlim([0 2*Amp]);
function [finalParm] = fitGauss2D(xVal2D,yVal2D,zVal2D,initParm)
% *purpose*
% fit a data to a 2D rotated Gaussian function
% *inputs*
% xVal2D - [arbitrary units] define horizontal indices of zVal2D
% yVal2D - [arbitrary units] define vertical indices of zVal2D
% zVal2D - [arbitrary units] array of values to fit to 2D Gaussian
% fit parameters
% initParm(1) Amp - [units of zData] Amplitude
% initParm(2) xG - [units of xVals] xFWHM
% initParm(3) yG - [units of yVals] yFWHM
% initParm(4) theta - [deg] rotation angle of 2D Gaussian rotation
% initParm(5) x0 - [units of xVals] horizontal value of centroid
% initParm(6) y0 - [units of yVals] vertical value of centroid
% *outputs*
% finalParm - fit parameters
zFunc = @fitGauss2D;
finalParm = fminsearch(zFunc, initParm);
function [SSE, zFit] = fitGauss2D(params)
Amp = params(1);
xG = params(2).^2;
yG = params(3).^2;
theta = params(4);
x0 = params(5);
y0 = params(6);
xC = ((xVal2D-x0).*cosd(theta) - (yVal2D-y0).*sind(theta)).^2;
yC = ((xVal2D-x0).*sind(theta) + (yVal2D-y0).*cosd(theta)).^2;
zFit = Amp.*exp( -(xC./(2.0*xG)) - (yC./(2.0*yG)) );
SSE = sum((zFit(:) - zVal2D(:)).^2);
end
end

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!