Go to the MLX, M, PDF, or HTML version of this file. Go back to fan’s MEconTools Package, Matlab Code Examples Repository (bookdown site), or Math for Econ with Matlab Repository (bookdown site).
There are three unique values in ar_a, sum up the probabilities for each of the unique states. This is equivalent to sorting a matrix with a and prob, and computing sum for each.
ar_a = [3,2,1,3]';
ar_prob = [0.1,0.2,0.31,0.39]';
ar_sumprob = accumarray(ar_a, ar_prob);
tb_summed_prob = table(sort(unique(ar_a)), ar_sumprob);
disp(tb_summed_prob);
Var1 ar_sumprob
____ __________
1 0.31
2 0.2
3 0.49
Upon solving a model, if we look for the mass at certain choices or states, accumarray could help aggregate up probabilities
a1 = [1,1,2,2]
a1 = 1x4
1 1 2 2
a2 = [3,2,1,3]
a2 = 1x4
3 2 1 3
a3 = [1,2,3,3]
a3 = 1x4
1 2 3 3
a = [a1;a2;a3]'/2
a = 4x3
0.5000 1.5000 0.5000
0.5000 1.0000 1.0000
1.0000 0.5000 1.5000
1.0000 1.5000 1.5000
prob_a = zeros(size(a)) + 1/12
prob_a = 4x3
0.0833 0.0833 0.0833
0.0833 0.0833 0.0833
0.0833 0.0833 0.0833
0.0833 0.0833 0.0833
[ar_idx_full, ~, ar_idx_of_unique] = unique(a)
ar_idx_full = 3x1
0.5000
1.0000
1.5000
ar_idx_of_unique = 12x1
1
1
2
2
3
2
1
3
1
2
mt_idx_of_unique = reshape(ar_idx_of_unique, size(a))
mt_idx_of_unique = 4x3
1 3 1
1 2 2
2 1 3
2 3 3
accumarray(mt_idx_of_unique(:,1), prob_a(:,1))
ans = 2x1
0.1667
0.1667
accumarray(mt_idx_of_unique(:,2), prob_a(:,2))
ans = 3x1
0.0833
0.0833
0.1667
accumarray(mt_idx_of_unique(:,3), prob_a(:,3))
ans = 3x1
0.0833
0.0833
0.1667