Matrix size is A(100*100). Weighted average of the 4 elements.

조회 수: 5 (최근 30일)
Karthik Nagaraj
Karthik Nagaraj 2018년 12월 5일
댓글: Jan 2018년 12월 6일
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
Stephen23
Stephen23 2018년 12월 6일
편집: Stephen23 2018년 12월 6일
"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
Karthik Nagaraj 2018년 12월 6일
편집: Karthik Nagaraj 2018년 12월 6일
Thanks a lot!
This gives the desired result!
Instead of 2x2 matrix, can you generalize the above solution for KXL matrix order, which is a submatrix whose averages are to be determined similarly.
I have reframed the question. Sorry for confusing 2x2 with 4x4. It is a 4 element sub matrix with 2x2 order and weighting is not actually involved.
In the case of 100*100 matrix; the subset of 2x2 window matrices reduce the 100x100 to 50x50 matrix (100/2=50). These 2x2 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. Similarly, for the next set I have to take third and fourth element of the first row and same elements of the second row and average it. This goes on until the 99th and 100th element of the first row and second row. All these averaged results are to be stored in the first row of a new matrix.
Likewise for the next set I have to take 3rd and 4th row and next 5th and 6th and so on upto 99th and 100th row. All these averaged results are to be stored in a new matrix whose dimensions are 25x25.
Note that the set of elements are nowhere repeated!
for loops with two variables and some more usual methods I have tried but of no use!
So I don't want to move the 2x2 window over by a pixel each time, I want to move it in "jumps" of 2x2 sub-matrix.
Please help me solving this.

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

채택된 답변

Jan
Jan 2018년 12월 6일
편집: Jan 2018년 12월 6일
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
Karthik Nagaraj
Karthik Nagaraj 2018년 12월 6일
Mr Jan,
Thank you so much for your time and efforts. Finally your solution works as mentioned. I had not changed one variable in your code for the requirement of a problem, that was the reason for me not getting the solution.
I correct myself, its 50x50 tiles and I misinterpreted the term 'Weighting'.
Thanks again.
Thank you one and all for helping me out!
Jan
Jan 2018년 12월 6일
@Karthik: You are welcome. Helping to solve problems is the purpose of this forum :-)

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

추가 답변 (1개)

Image Analyst
Image Analyst 2018년 12월 5일
Try this:
kernel = ones(4)/16;
localMeans = conv2(A, kernel, 'same');
  댓글 수: 4
Karthik Nagaraj
Karthik Nagaraj 2018년 12월 5일
Thanks again for responding!
I am finding it difficult to understand the conv() function role here.
In the case of 100*100 matrix; the subset of 4x4 window matrices reduce the 100x100 to 25x25 matrix (100/4=25). 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. Similarly, for the next set I have to take third and fourth element of the first row and same elements of the second row and average it. This goes on until the 99th and 100th element of the first row and second row. All these averaged results are to be stored in the first row of a new matrix.
Likewise for the next set I have to take 3rd and 4th row and next 5th and 6th and so on upto 99th and 100th row. All these averaged results are to be stored in a new matrix whose dimensions are 25x25.
Note that the set of elements are nowhere repeated!
for loops with two variables and some more usual methods I have tried but of no use!
Please help me solving this.
Image Analyst
Image Analyst 2018년 12월 6일
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.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by