【发布时间】:2017-08-22 22:04:37
【问题描述】:
概述
n×m 矩阵A 和n×1 向量Date 是函数S = sumdate(A,Date) 的输入。
该函数返回一个n×m 向量S,使得S 中的所有行对应于同一日期的A 行的总和。
例如,如果
A = [1 2 7 3 7 3 4 1 9
6 4 3 0 -1 2 8 7 5]';
Date = [161012 161223 161223 170222 160801 170222 161012 161012 161012]';
那么我希望返回的矩阵S 是
S = [15 9 9 6 7 6 15 15 15;
26 7 7 2 -1 2 26 26 26]';
-
因为元素
Date(2)和Date(3)是一样的,所以我们有-
S(2,1)和S(3,1)都等于A(2,1)和A(3,1)之和 -
S(2,2)和S(3,2)都等于A(2,2)和A(3,2)之和。
-
-
因为
Date(1)、Date(7)、Date(8)和Date(9)元素是一样的,所以我们有S(1,1)、S(7,1)、S(8,1)、S(9,1)等于A(1,1)、A(7,1)、A(8,1)、A(9,1)之和S(1,2)、S(7,2)、S(8,2)、S(9,2)等于A(1,2)、A(7,2)、A(8,2)、A(9,2)之和
S([4,6],1) 和 S([4,6],2) 相同
由于元素Date(5)不重复,所以S(5,1) = A(5,1) = 7和S(5,2) = A(5,2) = -1。
到目前为止我写的代码
这是我对这个任务的代码的尝试。
function S = sumdate(A,Date)
S = A; %Pre-assign S as a matrix in the same size of A.
Dlist = unique(Date); %Sort out a non-repeating list from Date
for J = 1 : length(Dlist)
loc = (Date == Dlist(J)); %Compute a logical indexing vector for locating the J-th element in Dlist
S(loc,:) = repmat(sum(S(loc,:)),sum(loc),1); %Replace the located rows of S by the sum of them
end
end
我使用 A 和 Date 在我的电脑上测试了它,并带有这些属性:
size(A) = [33055 400];
size(Date) = [33055 1];
length(unique(Date)) = 2645;
我的电脑用了大约 1.25 秒来执行任务。
这个任务在我的项目中执行了数十万次,因此我的代码太耗时了。如果我可以消除上面的for循环,我认为性能会得到提升。
我发现了一些内置函数,它们可以进行特殊类型的求和,例如 accumarray 或 cumsum,但我仍然对如何消除 for 循环没有任何想法。
感谢您的帮助。
【问题讨论】:
-
注意:你应该使用
.'转置矩阵,而不是复共轭转置' -
非常感谢您的编辑和建议。你是对的,我应该改用
.',因为在输入包含复数的情况下我不需要任何共轭。 -
没问题,我最终为您进行了编辑,但以后当您的文本中有代码时,请尝试使用
code formatting而不是 粗体格式,这样可以事情清晰了很多