Clear Filters
Clear Filters

double summation of a matrix

6 views (last 30 days)
adi
adi on 5 May 2020
Commented: adi on 5 May 2020
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 Comments
Stephen23
Stephen23 on 5 May 2020
What is supposed to happen at the boundaries?
adi
adi on 5 May 2020
i thought to pad with zeroes...or duplicate other matrix row/col.
im working on an image anhancing

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 5 May 2020
Edited: John D'Errico on 5 May 2020
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 Comment
adi
adi on 5 May 2020
thank you very much! it is very useful!

Sign in to comment.

More Answers (2)

KSSV
KSSV on 5 May 2020
Edited: KSSV on 5 May 2020
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 on 5 May 2020
Edited: Stephen23 on 5 May 2020
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

Community Treasure Hunt

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

Start Hunting!