Calculating mean of matrix elements
이전 댓글 표시
Hey guys,
I need a bit of help regarding calculating the mean of elements in a matrix. I want to calculate the mean of each value and the eight values surrounding it (excluding elements in the first / last rows and first/ last columns), then display the mean values in a new matrix, with the first / last rows and columns from the original matrix in place. Here is some code that I have put together but cannot seem to get to work:
x = 300x300 matrix
for i = x(2:299, 2:299)
x = [rows, cols];
for rows = 0:255
for cols = 0:255
q = [[rows, cols], [rows-1, cols-1], [rows-1, cols], [rows-1, cols+1], [rows, cols+1], [rows+1, cols-1], [rows+1, cols], [rows+1, cols+1]];
m = mean(q);
end
end
end
n = [m + x(1, 1:300) + x(300, 1:300) + x(1:300, 1) + x(1:300, 300)];
댓글 수: 2
Andrei Bobrov
2011년 5월 19일
http://www.mathworks.com/matlabcentral/answers/7567-accessing-surrounding-elements-in-an-array
J.C.
2011년 5월 19일
답변 (1개)
Sean de Wolski
2011년 5월 19일
x = magic(300); %example matrix
x2 = conv2(x,ones(3)/9,'valid'); %windowed mean of 3x3 grid
x(2:end-1,2:end-1) = x2; %insert
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!