Acquiring blocks of data

1 view (last 30 days)
I have a 100x100 matrix (Matrix A) and ive been trying to get a matrix B. Where each cell in matrix B contains an array of all the value that surround the corresponding cell in Block A in terms of a block with size N (Lets say its 2 for now). The results should be matrix B which is 100x100 where each cell contain an array of the surrounding data points. I hope this makes sense.
I would appreciate any help
jonas on 5 Jul 2020
What are you going to do with matrix B afterwards? Perhaps conv2 or blockproc functions could solve the problem without building matrix B.

Sign in to comment.

Accepted Answer

Vinai Datta Thatiparthi
Vinai Datta Thatiparthi on 10 Jul 2020
Hey Ahmed,
Firstly, since your output should be a collection of arrays of different values and dimensions, using cell arrays is the correct way to go about solving the problem. This code is a simplified version of what you're trying to do:
matIn = randi(5,5,5); % The input matrix
% Insert the 100x100 matrix in your case
cellOut = cell(5,5); % Cell array to hold the output
matRef = zeros(size(matIn));
for i=1:numel(cellOut)
matRef(:) = 0;
matRef(i) = 1;
% Use convolution to get the neighbors
cellOut{i} = matIn(conv2(matRef,[1,1,1;1,0,1;1,1,1],'same')>0)';
Finally, to get the neighbors of any element in matIn with the indices (i,j), simply use
Further, to echo @Jonas thoughts, consider using conv2 in your application directly to get what you want, instead of having to go through these steps.
Hope this helps!

More Answers (0)




Community Treasure Hunt

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

Start Hunting!