这几天我想知道如何在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
现在,使用 for 循环遍历行数 n 的实现或多或少是简单的。我现在正在寻找解决方案,它不需要这个可能很长的循环,而是使用向量算术。我有一些想法,但每个想法都遇到了一些问题,所以现在不再提及。如果有人曾经遇到过类似的情况,请分享您的解决方案。
Thanks.
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)
result:
B
A
D
A
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)