Info
This question is closed. Reopen it to edit or answer.
Creating a matrix where each row is the average of rows of another matrix.
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Andrei Bobrov
on 31 May 2017
Edited: Andrei Bobrov
on 31 May 2017
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 Comments
Image Analyst
on 5 Jun 2017
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
on 5 Jun 2017
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
on 4 Jun 2017
Edited: Andrei Bobrov
on 5 Jun 2017
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 Comments
This question is closed.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!