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!