29 views (last 30 days)

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

Jan
on 6 Dec 2018

@Karthik: Please try to concentrate. The question if you mean 2x2 or 4x4 sub matrices is still not clear. See:

subset of 2x2 window matrices reduce the 100x100 to 25x25 matrix (100/4=25)

No, a 2x2 window of a 100x100 matrix creates 50x50 tiles, not 25x25.

The code I have posted runs without an error. If you get an error in reshape you do run a different code. If you do not post it, I cannot find the problem. Please try it again:

n = 2;

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);

This can be expanded easily to the general case:

K = 2;

L = 4;

X = rand(100, 100);

Y = reshape(X, [K, 100/K, L, 100/L]);

Y = permute(Y, [2, 4, 1, 3]); % Move dims of length n together

Y = reshape(Y, [100/K, 100/L, K*L]); % Combine to subvectors of length n*n

Result = mean(Y, 3);

If the size of the matrix is not a multiple of the sub-matrix sizes, this must fail. In this FileExchange submission you find a fast C-Mex function to compute the results and the same function as M-file also, which crops the right and bottom columns and rows, if the size is not a multiple of K or L: https://www.mathworks.com/matlabcentral/fileexchange/24812-blockmean

You still did not explain the "weighting".

Jan
on 6 Dec 2018

@Karthik: You are welcome. Helping to solve problems is the purpose of this forum :-)

Image Analyst
on 5 Dec 2018

Try this:

kernel = ones(4)/16;

localMeans = conv2(A, kernel, 'same');

Image Analyst
on 5 Dec 2018

Image Analyst
on 6 Dec 2018

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

Start Hunting!
## 5 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_646665

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_646665

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_646743

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_646743

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_647043

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_647043

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_647055

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_647055

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_647066

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/434008-matrix-size-is-a-100-100-weighted-average-of-the-4-elements#comment_647066

Sign in to comment.