double summation of a matrix

조회 수: 5 (최근 30일)
adi
adi 2020년 5월 5일
댓글: adi 2020년 5월 5일
can anyone help me perform this summation in matlab? S is a 330X332 uint8 matrix.
i tried with 2 for loops but it doesnt work.
thank you!
  댓글 수: 2
Stephen23
Stephen23 2020년 5월 5일
What is supposed to happen at the boundaries?
adi
adi 2020년 5월 5일
i thought to pad with zeroes...or duplicate other matrix row/col.
im working on an image anhancing

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
John D'Errico 2020년 5월 5일
편집: John D'Errico 2020년 5월 5일
Of course it does work to use loops. Therefore, your loops were not written correctly. :-) However, it is far easier to write the entire process in one simple line of code.
U = conv2(double(S),ones(5))/25;
Note that I converted S using the double command, since you will divide by 25 at the end. Anyway, forming that sum would have likely caused uint8 overflows if you did it inside loops, unless you were careful to avoid them.
However, there is likely a problem in this result, since you have not told us what to do around the boundaries of the matrix. What is shown above will produce a new matrix that is size 334X336, so 4 rows and 4 columns larger. Why?
The answer is what would you compute whem m=n=1? conv2 makes the assumption that around the borders, it implicitly expands your matrix with zeros as necessary. But this means the sum around the borders will look strange. So I would predict you will not be happy, because around the borders, you will be forming a sum of less than 25 elements, yet you will be always dividing by 25.
So, instead, I would suggest you decide exactly what to do around the perimeter. This would probably be described as the boundary conditions for this operation.
If I had to guess what you most likely want to do, it is this:
U = conv2(double(S),ones(5),'same') ./ conv2(ones(size(S)),ones(5),'same');
I'll allow you to figure out why this operation works, even around the perimeter of the array, forming the desired average of elements. Perhaps this small example will help:
conv2(ones(7),ones(5),'same')
ans =
9 12 15 15 15 12 9
12 16 20 20 20 16 12
15 20 25 25 25 20 15
15 20 25 25 25 20 15
15 20 25 25 25 20 15
12 16 20 20 20 16 12
9 12 15 15 15 12 9
The resulting matrix in U will be the same size and shape as S. If you want the result to be essentially another image (as I assume this is what you are working on), stored back into uint8, you could then convert it back into uint8.
U = uint8(U);
  댓글 수: 1
adi
adi 2020년 5월 5일
thank you very much! it is very useful!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

KSSV
KSSV 2020년 5월 5일
편집: KSSV 2020년 5월 5일
s = rand(330,332) ;
m = 5 ;n = 5 ;
u = 0 ;
for i = -2:2
for j = -2:2
u = u+s(m+i,n+j) ;
end
end

Stephen23
Stephen23 2020년 5월 5일
편집: Stephen23 2020년 5월 5일
By far the easiest solution is to use conv2, e.g. where S is your array:
>> S = randi(255,330,332,'uint8');
>> U = conv2(double(S),ones(5,5),'same')/25;
And checking some random location, e.g. m=7 and n=23:
>> sum(sum(S(7-2:7+2,23-2:23+2)))/25
ans =
121.56
>> U(7,23)
ans =
121.56

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by