Matrix size is A(100*100). Weighted average of the 4 elements.
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Karthik Nagaraj
el 5 de Dic. de 2018
Comentada: Jan
el 6 de Dic. de 2018
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 comentarios
Stephen23
el 6 de Dic. de 2018
Editada: Stephen23
el 6 de Dic. de 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.
Respuesta aceptada
Jan
el 6 de Dic. de 2018
Editada: Jan
el 6 de Dic. de 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 comentarios
Jan
el 6 de Dic. de 2018
@Karthik: You are welcome. Helping to solve problems is the purpose of this forum :-)
Más respuestas (1)
Image Analyst
el 5 de Dic. de 2018
Try this:
kernel = ones(4)/16;
localMeans = conv2(A, kernel, 'same');
4 comentarios
Image Analyst
el 6 de Dic. de 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.
Ver también
Categorías
Más información sobre Matrix Indexing en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!