是否有一个 accumarray() 将矩阵作为 `val`? [英] Is there an accumarray() that takes matrix as `val`?

查看:20
本文介绍了是否有一个 accumarray() 将矩阵作为 `val`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

accumarray()val 参数必须是一个向量.在我的情况下,我需要对矩阵的列求和(或求平均值).有没有实现这个的功能或方法?

accumarray()'s val argument must be a vector. In my case I need columns of a matrix to be summed (or averaged). Is there a function or a method to achieve this?

我现在正在做的是在 for 循环中我分别对列值求和:

What I am doing now is in a for loop I am summing column values separately:

for iCol = 1:nCols
    means(:,iCol) = accumarray(labels', X(:,iCol));
end

推荐答案

一种解决方案是复制 labels 中的行索引并添加另一列列索引.然后你可以将 X 重塑为列向量并应用 accumarray 一次:

One solution is to replicate the row indices in labels and add another column of column indices. Then you can reshape X into a column vector and apply accumarray once:

labels = [repmat(labels(:),nCols,1) ...            % Replicate the row indices
          kron(1:nCols,ones(1,numel(labels))).'];  % Create column indices
totals = accumarray(labels,X(:));  % I used "totals" instead of "means"


A = accumarray(subs,val) 用于列向量 subs 和向量 val 通过在 val 中添加数字来工作(i) 到输出列向量 A 中行 subs(i) 的总和.但是,subs 可以包含的不仅仅是行索引.它可以包含多个维度的下标索引以在输出中分配值.此功能允许您处理输入 val,它是矩阵而不是向量.

A = accumarray(subs,val) for a column vector subs and vector val works by adding the number in val(i) to the total in row subs(i) in the output column vector A. However, subs can contain more than just row indices. It can contain subscript indices for multiple dimensions to assign values to in the output. This feature is what allows you to handle an input val that is a matrix instead of a vector.

首先,可以使用 val 的输入改造成列向量rel="nofollow noreferrer">冒号运算符 X(:).接下来,为了跟踪输出中的哪一列应该放置 X(:) 中的值,我们可以修改输入 subs 以包含额外的列索引.为了说明这是如何工作的,我将使用以下示例输入:

First, the input for val can be reshaped into a column vector using the colon operator X(:). Next, in order to keep track of which column in the output the values in X(:) should be placed, we can modify the input subs to include an additional column index. To illustrate how this works, I'll use these sample inputs:

labels = [3; 1; 1];
X = [1 2 3; ...
     4 5 6; ...
     7 8 9];
nCols = 3

下面是上面代码中的变量最终的样子:

And here are what the variables in the above code end up looking like:

labels = 3 1    X(:) = 1    totals = 11 13 15
         1 1           4              0  0  0
         1 1           7              1  2  3
         3 2           2
         1 2           5
         1 2           8
         3 3           3
         1 3           6
         1 3           9

注意,例如,原本在 X 第一列中的值 1 4 7 只会在输出的第一列中累加,如由 labels 的第二列的前三行中的那些表示.结果输出应该与您使用问题中的代码获得的输出相同,在该代码中您循环遍历每一列以执行累加.

Notice, for example, that the values 1 4 7 that were originally in the first column of X will only be accumulated in the first column of the output, as denoted by the ones in the first three rows of the second column of labels. The resulting output should be the same as what you would have gotten by using the code in the question where you loop over each column to perform the accumulation.

这篇关于是否有一个 accumarray() 将矩阵作为 `val`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆