Azzera filtri
Azzera filtri

Info

Questa domanda è chiusa. Riaprila per modificarla o per rispondere.

Creating a matrix where each row is the average of rows of another matrix.

1 visualizzazione (ultimi 30 giorni)
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.

Risposte (2)

Andrei Bobrov
Andrei Bobrov il 31 Mag 2017
Modificato: Andrei Bobrov il 31 Mag 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 Commenti
Image Analyst
Image Analyst il 5 Giu 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
Andrei Bobrov il 5 Giu 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
Image Analyst il 4 Giu 2017
Modificato: Andrei Bobrov il 5 Giu 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.

Questa domanda è chiusa.

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by