Info
Cette question est clôturée. Rouvrir pour modifier ou répondre.
Creating a matrix where each row is the average of rows of another matrix.
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Andrei Bobrov
le 31 Mai 2017
Modifié(e) : Andrei Bobrov
le 31 Mai 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 commentaires
Image Analyst
le 5 Juin 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
le 5 Juin 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
le 4 Juin 2017
Modifié(e) : Andrei Bobrov
le 5 Juin 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 commentaires
Cette question est clôturée.
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!