Matlab 三维矩阵中周围单元的索引和值
我想返回3d矩阵中围绕一个单元格的8个单元格的索引和值Matlab 三维矩阵中周围单元的索引和值,matlab,matrix,Matlab,Matrix,我想返回3d矩阵中围绕一个单元格的8个单元格的索引和值 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_
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,:))
这适用于矩阵中心的值,但如果pos位于边缘,则会中断。(例如,如果pos是[3,4,5]
,则周围的位置将包括[3,4,6]
,这是不允许的)
很明显,我可以删除周围的位置值大小(mat),但这似乎不是一个可怕的MATLABian方法。有什么想法吗 这是一个整理过的版本。干杯
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)))
mat=rand(5,5,5);
N=尺寸(垫)
如果长度(N)<3 | |长度(N)>3;错误(“输入必须是三维的”);结束;
位置=[1 3 5]
周围环境=垫(最大(位置(1)-1,1):最小(位置(1)+1,N(1)),最大(位置(2)-1,1):最小(位置(2)+1,N(2)),最大(位置(3)-1,1):最小(位置(3)+1,N(3)))
编辑:添加了一个错误陷阱。与讨论的解决方案相同,但扩展到多个(任意)维度:
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
或者,如果您喜欢短代码,则循环将更改为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{:});
我找到这篇文章是因为我需要获取矩阵中选定点的周围索引。这里的答案似乎返回了一个周围值的矩阵,但问题也对周围的指数感兴趣。我可以用“try/catch”语句来实现这一点,而我以前不知道MATLAB中存在这样的语句。对于二维矩阵,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's 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) =[];
我希望有人能找到相关的信息。根据您的问题,解决方法可能是在矩阵的各个面添加NAN。然后,您的索引工作正常,您只需关心在分析中忽略NAN。您可以使用此处讨论的相同方法:@H.Muster好主意,但我想探讨的是只获取相关索引,而不是操纵整个矩阵-性能非常重要important@Alex:这就是我写“取决于你的问题”的原因。如果要反复查询同一个矩阵,填充方法可能会产生更好的性能。如果只对许多不同的矩阵进行少量查询,它的性能可能会比较差。我完全同意@H.Muster-在数组中填充一个光环,其中包含一个标识元素,用于单元格模板上的操作,通常在操作计数增加时,这种类型的操作的性能要高得多。因为不能保证它是方形的,所以也可以使用v=size(mat),然后是v(1),v(2)在错误陷阱的位置,只有当你有一个
nx1xnx…
矩阵时,错误陷阱才会起作用;最高维度的单例维度不会自动显示(也不会由size
返回)。(因为理论上每个矩阵都是nx1x1x…
矩阵。)@guntherstruy如果你是正确的。对不起,我今天身体不太好,是吗?:-)我已经修复了错误陷阱。整洁的扩展-非常值得a+1。现在这个帖子肯定不是你提供的链接的副本:-)当你发帖子的时候,你就开始打字了,你想不出来。。不管怎么说,它涵盖了相同的内容,不知道为什么格式会如此糟糕。对不起,我不知道怎么修理。