Acquiring blocks of data

1 次查看(过去 30 天)
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 个评论
Voss
Voss 2020-7-5
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
jonas 2020-7-5
What are you going to do with matrix B afterwards? Perhaps conv2 or blockproc functions could solve the problem without building matrix B.

请先登录,再进行评论。

采纳的回答

Vinai Datta Thatiparthi
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 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by