Matrix size is A(100*100). Weighted average of the 4 elements.
7 views (last 30 days)
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
Stephen23
on 6 Dec 2018
Edited: Stephen23
on 6 Dec 2018
"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.
Accepted Answer
Jan
on 6 Dec 2018
Edited: Jan
on 6 Dec 2018
In the original question and the image you mentiones 2x2 submatrices. In the commend you talk of 4x4 submatrices. This is not clear in this sentence also:
These 4x4 matrices are unique; i.e., I have to take the first two elements of the first row and same elements of the second row and average it.
Here I used n=4, but perhaps you want n=2:
n = 4;
X = rand(100, 100);
Y = reshape(X, [n, 100/n, n, 100/n]);
Y = permute(Y, [2, 4, 1, 3]); % Move dims of length n together
Y = reshape(Y, [100/n, 100/n, n*n]); % Combine to subvectors of length n*n
Result = mean(Y, 3); % Mean over 3rd dimension
5 Comments
Jan
on 6 Dec 2018
@Karthik: You are welcome. Helping to solve problems is the purpose of this forum :-)
More Answers (1)
Image Analyst
on 5 Dec 2018
Try this:
kernel = ones(4)/16;
localMeans = conv2(A, kernel, 'same');
4 Comments
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.
See Also
Categories
Find more on Matrix Indexing 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!