有几天,我在想如何在matlab中有效地实现m个专家的加权多数投票。这是我想要的一个例子。假设我们有3个具有权重向量的专家
w=[7 2 6]
假设他们对选项A/B/C/D投票n次,例如,我们得到以下n x m投票矩阵,其中列是每个专家的投票。
A B B
C A A
D B A
A A C
现在我想计算每一行的加权多数票。我们通过添加投票给每个选项的专家的权重,并选择最大的权重来计算它。例如,在第一行中,选项A的累积权重为7(专家1的投票),B的累积权重为8(专家2和3的投票),因此最终投票为B。因此,我们得到以下累积权重矩阵和最终投票:
A B C D
- - - -
7 8 0 0 -> B
8 0 7 0 -> A
6 2 0 7 -> D
9 0 6 0 -> A
现在,在行数n上使用for循环的实现或多或少很简单。我现在正在寻找解决方案,它不需要这个潜在的冗长循环,而是使用向量算法。我有一些想法,但每个想法都遇到了一些问题,所以现在不提了。如果有人以前遇到过类似的情况,请分享你的解决方案。
谢谢。
发布于 2013-03-19 12:24:28
w=[7 2 6];
votes = ['A' 'B' 'B'
'C' 'A' 'A'
'D' 'B' 'A'
'A' 'A' 'C'];
options = ['A', 'B', 'C', 'D']';
%'//Make a cube of the options that is number of options by m by n
OPTIONS = repmat(options, [1, size(w, 2), size(votes, 1)]);
%//Compare the votes (streched to make surface) against a uniforma surface of each option
B = bsxfun(@eq, permute(votes, [3 2 1]) ,OPTIONS);
%//Find a weighted sum
W = squeeze(sum(bsxfun(@times, repmat(w, size(options, 1), 1), B), 2))'
%'//Find the options with the highest weighted sum
[xx, i] = max(W, [], 2);
options(i)
结果:
B
A
D
A
https://stackoverflow.com/questions/15498368
复制