Matrix size is A(100*100). Weighted average of the 4 elements.
Show older comments
How to store the result in a matrix which contains the average the 4 elements in the cluster A11, A12,A21,A22 and similarly A13,A14.A23,A24 and goes on? The resultant matrix should be 25*25
5 Comments
What are your inputs? What is the wanted output? What does "A12" mean? I hope you do not want to create a pile of variables dynamically, which have a hidden index in their name. See TUTORIAL: Eval . What does "weighted" mean? What are the weighting factors? What do you call a "cluster"?
Karthik Nagaraj
on 5 Dec 2018
Jan
on 6 Dec 2018
This image does not clarify, what you want to do. Please explain the detail of weighting the average.
"The resultant matrix should be 25*25"
Following your description the resulting matrix would actually be 50x50. Consider a simpler 10x10 matrix:
>> A = randi(9,10,10) % 10x10
A =
5 5 8 2 7 4 4 2 1 6
9 7 2 5 9 8 6 8 2 2
1 5 6 8 6 8 7 4 3 4
7 9 4 4 3 2 6 6 7 1
3 8 8 7 2 3 5 8 2 4
1 6 9 8 1 8 6 4 8 5
7 9 3 3 1 6 5 9 3 8
2 6 7 8 3 9 9 1 8 9
6 1 6 3 7 5 6 4 9 7
9 2 6 7 6 8 4 1 5 5
>> M = (A(1:2:end,1:2:end)+A(2:2:end,1:2:end)+A(1:2:end,2:2:end)+A(2:2:end,2:2:end))/4
M =
6.5000 4.2500 7.0000 5.0000 2.7500
5.5000 5.5000 4.7500 5.7500 3.7500
4.5000 8.0000 3.5000 5.7500 4.7500
6.0000 5.2500 4.7500 6.0000 7.0000
4.5000 5.5000 6.5000 3.7500 6.5000
>> size(M) % resulting matrix is 5x5
ans =
5 5
>> mean([5,5,9,7]) % check first block
ans = 6.5000
So far it is not clear where any weighting is involved. Please clarify how the weighting is defined and what it should be applied to.
Karthik Nagaraj
on 6 Dec 2018
Edited: Karthik Nagaraj
on 6 Dec 2018
Accepted Answer
More Answers (1)
Image Analyst
on 5 Dec 2018
Try this:
kernel = ones(4)/16;
localMeans = conv2(A, kernel, 'same');
4 Comments
Karthik Nagaraj
on 5 Dec 2018
Edited: Karthik Nagaraj
on 5 Dec 2018
Image Analyst
on 5 Dec 2018
Only one 4x4 window will fit into a 4x4 array. If you slide it around, then some part will be off the array. So then you will have less than 16 elements in. So you have to decide what to do with the missing elements. conv() assumes they're zero. If you really need to have the window "shrink" when it gets close the the edge, you'll have to do conv2() twice. Once to sum up the values of the array, and once to count the number of elements at each position, and then you divide those two images. Let me know if you need this. Usually it's not needed since the array is much bigger than the filter window and what happens near the edges does not really matter that much.
Karthik Nagaraj
on 5 Dec 2018
Image Analyst
on 6 Dec 2018
So you don't want to move the 4x4 window over by a pixel each time, you want to move it in "jumps" of 4. That's quite a different thing. You can do it in jumps if you use blockproc(). Study the attached examples for a variety of ways to use it. Adapt as needed.
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!