通过在 matlab 中根据指定顺序对样本行求和来创建经验联合概率矩阵的有效方法

An efficient way for creating an empirical joint probability matrix by summing rows of samples according to a specified order in matlab

假设我有一个样本矩阵 samples (n_samples x n1) 和一个标签向量 labels (n_samples x 1),其中标签位于范围 [1:n2]

我正在寻找一种有效的方法来创建大小为 n2 x n1 的经验联合概率矩阵 P。 对于每个样本 i,我们在 labels(i) 指示的位置将其行 samples(i, :) 添加到 P

即(伪代码)

for i = 1:n_samples
   P(l(i), :) += M(i, :)

是否有一个杀手级的 matlab 命令可以做到这一点?而不是 for 循环或 arrayfun?

关注@BillBokeey 评论:这是解决方案

[xx, yy] = ndgrid(labels,1:size(samples,2));
P = accumarray([xx(:) yy(:)],samples(:));