[英]Matlab Accumarray for 3D matrix
我真的希望文档在Matlab中将Accumarray用于ND尺寸矩阵时更加清晰。 无论如何,我在这里很困惑,正在寻找一些建议。 这是挑战:
我有一个3D数据矩阵。
对于此示例,我们假设每个观测值都是每5分钟间隔流经仪表的水量。 但是,我现在希望将数据重新采样为N分钟的间隔(显然是5的倍数)。 让我们选择NMINS = 15。
因此,我想做的是找出NMINS分钟间隔内的水量总和。 也就是说,ROWS和COLUMNS不会更改,但是PAGES的尺寸和值将被抽取/汇总。
我可以获取页面的分组值。 也就是说,我需要分组的值。 如果一天只有一条河,那没问题。 但是,我有数百天和数十条河流。
我已经走了这么远:
创建测试时间矢量
NMINS = 15; % Bucket by every 15 mins or 20, etc.
N5MINS = 5 * 12 * 24 * 2; % Keep small - Two days of 5 min datenums
dnums = datenum(2016,3,20,0,0:5:N5MINS,0);
% Trim dnums to start at random time for edge case and keep only mins
mins = rem(dnums(25:end-30),1)'; % Create column vector
创建用于测试的随机矩阵
rng(123); % Set seed for reproducibility
X = randi(100,12,9,length(mins)); % Test matrix
以分钟为单位查找时间
[~,~,~,H,M] = datevec( mins );
H = 60 .* (H - H(1));
现在查找与我们的桶相对应的所有时间
idxMIN = mod( M+H, NMINS )==0;
idxNewP = idxMIN; % This is used to align the new river matrix
[R,C,P] = size(X); % We'll drop P and use newP
newP = sum(idxNewP); % Number of PAGES in final matrix (new)
% Final newX will have dimensions [R,C,newP]
重设分组索引
% Must shift one all as minute intervals represent data UP to that point
% for the actual grouping of data. Test if first bucket is a
% match and set accordingly
if idxMIN(1)
idxMIN = [1; idxMIN(1:end-1)];
subs = cumsum(idxMIN);
else
idxMIN = [0; idxMIN(1:end-1)];
subs = cumsum(idxMIN) + 1 ;
end
补充:小组人数将不一致,我不能(很难)做出这种假设。 运行以上步骤后,请考虑以下内容。
tsttbl = table();
tsttbl.dnumstr = datestr(mins(1:5));
tsttbl.Mins = M(1:5);
tsttbl.subs = subs(1:5);
tsttbl
输出显示第一组的大小为1:
tsttbl =
dnumstr Mins subs
________ ____ ____
2:00 AM 0 1
2:05 AM 5 2
2:10 AM 10 2
2:15 AM 15 2
2:20 AM 20 3
最后,最终,我需要传递自定义函数。 上面是一个玩具示例,可以快速说明问题。 抱歉,我不清楚。
结束添加
这就是我绊倒的地方...
如何设置要在每个页面上应用的subs值以使用accumarray? 我完全被文档弄糊涂了。 还是这实际上是错误的方法? 为了值得,我正在使用Matlab 2015b。
老实说,任何帮助将不胜感激。
替代解决方案这使我在回家的路上感到震惊 。 Meshgrid是我的朋友。
一旦运行了上面的单元格(请注意,我更改了矩阵X的大小),我们将为整个矩阵创建索引,其中页面(即时间)的“索引”由“ subs”中的值给出。 为此,我使用meshgrid。
[days,rivers,pages] = meshgrid(1:C,1:R,subs);
grpvals = [rivers(:) days(:) pages(:)];
tst = accumarray(grpvals,X(:),[R C newP],@sum);
可能不是最高效的内存,因为我必须本质上创建日期,河流和页面矩阵,然后最终创建一个包含这些矩阵的新grpvals数组。 但是,它的优势在于现在我可以使用accumarray并传递匿名函数,@ std等。
希望这对别人有帮助!
非常感谢路易斯。
您可以按以下方式进行汇总:
码:
X = randi(9,2,3,6); %// example data. 3D array.
G = 2; %// group size along 3rd dim. Divides size(X,3)
result = squeeze(sum(reshape(X, size(X,1), size(X,2), G, []), 3));
例如,对于G = 2
,
X(:,:,1) =
2 3 9
4 5 9
X(:,:,2) =
3 8 2
6 9 8
X(:,:,3) =
4 4 4
1 1 7
X(:,:,4) =
9 9 8
2 4 1
X(:,:,5) =
9 5 9
3 5 8
X(:,:,6) =
9 1 3
5 3 1
给
result(:,:,1) =
5 11 11
10 14 17
result(:,:,2) =
13 13 12
3 5 8
result(:,:,3) =
18 6 12
8 8 9
由于accumarray
不适用于多维数组(甚至是矩阵)作为第二个输入,因此可以沿此答案行使用矩阵乘法。 为此,您需要将3D数组的前两个维打包为一个维(最后将解压缩),然后从组索引中构建一个零一矩阵,该矩阵将通过矩阵乘法给出所需的结果。
X = randi(9,2,3,5); %// example data. 3D array.
subs = [1 2 2 1 1]; %// indices of groups. Groups may differ in size, and indices
%// need not be sorted
Y = reshape(X, [], size(X,3)); %// reshape into a matrix. Groups are along rows
M = full(sparse(1:numel(subs), subs, 1)); %// indicator matrix from group indices
result = reshape(Y*M, size(X,1), size(X,2), []); %// compute result and reshape
例如,
X(:,:,1) =
9 3 8
6 8 8
X(:,:,2) =
3 8 3
7 2 2
X(:,:,3) =
7 3 6
2 8 5
X(:,:,4) =
7 4 5
8 8 6
X(:,:,5) =
2 3 2
2 8 8
subs =
1 2 2 1 1
给
result(:,:,1) =
18 10 15
16 24 22
result(:,:,2) =
10 11 9
9 10 7
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.