QA 0011
Q: Solution to Central 851715, Alex asks how to alternately (horizontal/vertical) append matrix with averaged rows/columns distanced a given input range.
LINK TO QUESTION:
https://uk.mathworks.com/matlabcentral/answers/851715-how-to-sum-specific-elements-of-a-matrix/?s_tid=ans_lp_feed_leaf
As of June 15th, the supplied answers do not take into consideration to use ceil besides the required averaging, or the obtained figures do not match the sample supplied in the question.
I left the 2 top contributors to take the hit regarding the misleading operation mentioned by Alex, asking for 'sum' between rows and columns, which it turned to be an average that is required.
I also supply a general case, where the averaging distance can be changed, while the 2 contributors to this question have limited their answers to use the distance mentioned in the supplied sample.
clear all;close all;clc
N=6 % size square input matrix
% A=randi(20,400,N) % input matrix
If the answer to this question limits to start using randi for the test matrix, readers cannot tell whether the supplied script really match all the numbers in the target matrix supplied in the question.
So instead of randi let's literally use the sample matrix supplied in the question.
A =[ 29 46 54 84 447 457
158 3 316 98 352 354
387 211 63 159 278 279
348 328 67 158 92 157
62 362 49 108 106 83
65 266 71 125 38 311]
dn1=2 % span between horizontal averages For horizontal BLUE add-on
dn2=2 % span between vertical averages : For vertical Orange add-on
% 1st round; Vertical append BLUE
L1=zeros(1,N);
for s1=1:1:N-dn1-1
L1=[L1;ceil(mean( [A(s1,:);A(s1+dn1+1,:)]))];
end
L1(1,:)=[];
A1=[A;L1] % attach BLUE add-on
% 2nd round; Horizontal append ORANGE
L2=zeros(N+dn1+1,1);
for s2=1:1:N-dn2-1
L2=[L2 ceil(mean( [A1(:,s2),A1(:,s2+dn2+1)],2))];
end
L2(:,1)=[];
A2=[A1 L2] % attach ORANGE add-on