Indices and values ​​of surrounding cells in a 3d matrix

I would like to return the indices and values ​​of 8 cells surrounding a cell in a 3d matrix.

mat = rand(5,5,5);

% Cell of interest
pos = [3 3 3]
pos_val = mat(pos(1), pos(2), pos(3))

% Surrounding cells
surrounding_pos = [pos(1)-1:pos(1)+1; pos(2)-1:pos(2)+1; pos(2)-1:pos(2)+1]
surrounding_val = mat(surrounding_pos(1,:), surrounding_pos(2,:), surrounding_pos(3,:))

This works fine for values ​​in the center of the matrix, but it breaks if pos is on the edge. (For example, if pos was [3,4,5], environment_pos will include [3,4,6]what goes beyond)

I could obviously remove the environment_pos <0 or> size (mat) values, but that doesn't look like the awful MATLABian method. Any ideas?

+5
source share
3 answers

Here is the version given. Greetings.

mat = rand(5,5,5);
N = size(mat)
if length(N) < 3 || length(N) > 3; error('Input must be 3 dimensional'); end;
pos = [1 3 5]
surrounding_val = mat(max(pos(1)-1, 1):min(pos(1)+1, N(1)), max(pos(2)-1, 1):min(pos(2)+1, N(2)), max(pos(3)-1, 1):min(pos(3)+1, N(3))) 

EDIT : error added.

+4
source

The same solution that was discussed here , but expanded to several (any) dimensions:

mat = randi(10,5,5,5);
siz = size(mat );
N = numel(siz); % number of dimensions
M = 1; % surrounding region size

pos = [3 3 3];
pos_val = mat(pos(1), pos(2), pos(3));

surrounding_pos = cell(N,1);
for ii=1:N
    surrounding_pos{ii} = max(1,pos(ii)-M):min(siz(ii),pos(ii)+M);
end
surrounding_val2 = mat(surrounding_pos{:});

- , c/p max, min .

, , arrayfun:

surrounding_pos = arrayfun(@(ii) max(1,pos(ii)-M):min(siz(ii),pos(ii)+M), 1:N,'uni',false);
surrounding_val2 = mat(surrounding_pos{:});
+5

I found this post because I needed to capture the surrounding indices of the selected point in the Matrix. The answers here seem to return a matrix of surrounding values, but the question is also of interest to the surrounding indices. I was able to do this with the try / catch instructions, which I did not know about before, existed in MATLAB. For a 2D matrix Z:

%Select a node in the matrix
Current = Start_Node;

%Grab its x, y, values (these feel reversed for me...)
C_x = rem(Start_Node, length(Z));
if C_x ==0
    C_x =length(Z);
end
C_y = ceil(Start_Node/length(Z));
C_C = [C_x, C_y];

%Grab the node surrounding nodes.
try
    TRY = Z(C_x - 1, C_y);
    Top = Current -1;
catch
    Top = Inf;
end
try
    TRY = Z(C_x + 1, C_y);
    Bottom = Current +1;
catch
    Bottom = Inf;
end
try
    TRY = Z(C_x, C_y + 1);
    Right = Current + length(Z);
catch
    Right = Inf;
end
try
    TRY = Z(C_x, C_y - 1);
    Left = Current - length(Z);
catch
    Left = Inf;
end

surround = [Top, Bottom, Left, Right];
m = numel(surround == inf);
k = 1;
%Eliminate infinites.

surround(surround==inf) =[];

I hope someone finds this information relevant.

0
source

All Articles