MATLAB Answers

# Acquiring blocks of data

1 view (last 30 days)
Ahmed Abdulla on 5 Jul 2020
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

#### 2 Comments

Benjamin on 5 Jul 2020
Maybe an example A that's 5x5 or so and the corresponding output B (still assuming N == 2, say) would clarify exactly what you have in mind.
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.

### Answers (1)

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)';
end
Finally, to get the neighbors of any element in matIn with the indices (i,j), simply use
cellOut{i,j}
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!

#### 0 Comments

Sign in to comment.

R2019b

### Community Treasure Hunt

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

Start Hunting!