08. Resampling
Resampling
Resampling
Suppose that you have 5 particles, each with an importance weight. Compute the probability of drawing each particle in the new set. Use the C++ coding section and follow the instructions to print the results.
Start Quiz:
#include <iostream>
using namespace std;
double w[] = { 0.6, 1.2, 2.4, 0.6, 1.2 };//You can also change this to a vector
//TODO: Define a ComputeProb function and compute the Probabilities
int main()
{
//TODO: Print Probabilites each on a single line:
//P1=Value
//:
//P5=Value
return 0;
}
//One out of many possible solutions
#include <iostream>
using namespace std;
double w[] = { 0.6, 1.2, 2.4, 0.6, 1.2 };
double sum = 0;
void ComputeProb(double w[], int n)
{
for (int i = 0; i < n; i++) {
sum = sum + w[i];
}
for (int j = 0; j < n; j++) {
w[j] = w[j] / sum;
cout << "P" << j + 1 << "=" << w[j] << endl;
}
}
int main()
{
ComputeProb(w, sizeof(w) / sizeof(w[0]));
return 0;
}