Info
此问题已关闭。 请重新打开它进行编辑或回答。
Creating a matrix where each row is the average of rows of another matrix.
1 次查看(过去 30 天)
显示 更早的评论
Suppose I have 10*2 matrix A. I want to create 5*2 matrix B where the first row of A is the average of the first two rows of B, the second row of A is the average of the third and the fourth row of B, etc. Please advise.
0 个评论
回答(2 个)
Andrei Bobrov
2017-5-31
编辑:Andrei Bobrov
2017-5-31
One way:
a = [...
102 20
114 122
16 120
115 61
80 101
13 18
35 53
69 115
120 100
121 120];
out = splitapply(@mean,a,ceil((1:size(a,1))'/2));
other way:
[ii,jj] = ndgrid(ceil((1:size(a,1))'/2),1:size(a,2));
out = accumarray([ii(:),jj(:)],a(:),[],@mean);
third way:
n = mod(-size(a,1),2);
out = squeeze(mean(reshape([a;nan(n,2)]',size(a,2),2,[]),2))';
3 个评论
Image Analyst
2017-6-5
It's still not clear, but here is some of it:
% Compute average of column 3 and column 1
ave = (a(:, 3) + a(:, 1))/2
Andrei Bobrov
2017-6-5
A = [0.1 0 0.1 0;
0.1 0.1 0.1 0.1;
0.2 0 0.2 0;
0.2 0.1 0.2 0.1;
0.2 0.2 0.2 0.2];
[G,ii] = findgroups(A(:,1));
B = [ii,splitapply(@mean,A(:,3),G)];
Image Analyst
2017-6-4
编辑:Andrei Bobrov
2017-6-5
Here's yet another way using conv2 to use convolution to do a sliding mean of two rows:
outTemp = conv2(a, [1;1]/2, 'same'); % Moving mean
out = outTemp(1:2:end, :) % Skip every other one.
0 个评论
此问题已关闭。
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!