Main Content


Extract best state estimate and covariance from particles



State = getStateEstimate(pf) returns the best state estimate based on the current set of particles. The estimate is extracted based on the StateEstimationMethod property from the particleFilter object, pf.

[State,StateCovariance] = getStateEstimate(pf) also returns the covariance of the state estimate. The covariance is a measure of the uncertainty of the state estimate. Not all state estimation methods support covariance output. In this case, getStateEstimate returns StateCovariance as [ ].

The State and StateCovariance information can directly be accessed as properties of the particle filter object pf, as pf.State and pf.StateCovariance. However, when both these quantities are needed, using the getStateEstimation method with two output arguments is more computationally efficient.


collapse all

Create a particle filter, and set the state transition and measurement likelihood functions.

myPF = particleFilter(@vdpParticleFilterStateFcn,@vdpMeasurementLikelihoodFcn);

Initialize the particle filter at state [2; 0] with unit covariance, and use 1000 particles.

initialize(myPF, 1000, [2;0], eye(2));

Pick the mean state estimation and systematic resampling methods.

myPF.StateEstimationMethod = 'mean';
myPF.ResamplingMethod = 'systematic';
myPF = 
  particleFilter with properties:

           NumStateVariables: 2
                NumParticles: 1000
          StateTransitionFcn: @vdpParticleFilterStateFcn
    MeasurementLikelihoodFcn: @vdpMeasurementLikelihoodFcn
     IsStateVariableCircular: [0 0]
            ResamplingPolicy: [1x1 particleResamplingPolicy]
            ResamplingMethod: 'systematic'
       StateEstimationMethod: 'mean'
            StateOrientation: 'column'
                   Particles: [2x1000 double]
                     Weights: [1.0000e-03 1.0000e-03 1.0000e-03 ... ]
                       State: 'Use the getStateEstimate function to see the value.'
             StateCovariance: 'Use the getStateEstimate function to see the value.'

Assuming a measurement 2.1, run one predict and correct step.

[PredictedState, PredictedStateCovariance] = predict(myPF);
[CorrectedState, CorrectedStateCovariance] = correct(myPF,2.1);

Get the best state estimate and covariance based on the StateEstimationMethod property.

[State, StateCovariance] = getStateEstimate(myPF)
State = 2×1


StateCovariance = 2×2

    0.0175   -0.0096
   -0.0096    0.5394

Input Arguments

collapse all

Particle filter, specified as an object. See particleFilter for more information.

Output Arguments

collapse all

Best state estimate, defined as a vector based on the condition of the StateOrientation property:

  • If StateOrientation is 'row' then State is a 1-by-NumStateVariables vector

  • If StateOrientation is 'column' then State is a NumStateVariables-by-1 vector

Current estimate of state estimation error covariance, defined as an NumStateVariables-by-NumStateVariables array. StateCovariance is calculated based on the StateEstimationMethod. If you specify a state estimation method that does not support covariance, then the function returns StateCovariance as [ ].

Version History

Introduced in R2017b