Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 多维矩阵中两点的连通_Matlab_Matrix_Multidimensional Array_Distance_Shortest - Fatal编程技术网

Matlab 多维矩阵中两点的连通

Matlab 多维矩阵中两点的连通,matlab,matrix,multidimensional-array,distance,shortest,Matlab,Matrix,Multidimensional Array,Distance,Shortest,我正在尝试创建一个包含1和0的3d矩阵。我想通过在两点之间形成一条1的线,将两点连接在一起(最短距离) 它看起来像这样,但是是3d的 path_pixels = [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0; 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0]; 我可以用这个代码在2d中完成 clc; clear; %matrix size

我正在尝试创建一个包含1和0的3d矩阵。我想通过在两点之间形成一条1的线,将两点连接在一起(最短距离)

它看起来像这样,但是是3d的

path_pixels = [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0;

               0,0,0,1,0,0,0,0,0,0,0,0,0,0,0;

               0,0,0,0,1,0,0,0,0,0,0,0,0,0,0];
我可以用这个代码在2d中完成

clc;
clear;
%matrix size

A = zeros(70);
A(20,20) = 1;   %arbitrary point

B = zeros(70);
B(40,40) = 1; %arbitrary point

D1 = bwdist(A);
D2 = bwdist(B);

D_sum = D1 + D2 ;

path_pixels = imregionalmin(D_sum);

spy(path_pixels)

如何将此方法扩展到3d?

具体取决于您所说的“连接”是什么意思。但是,如果目标是起点和终点之间的一个单元格宽的区域,那么这看起来很像一条“一像素宽”的线,可以定义如下

% start and end point of line
a = [1 10 2];
b = [4 1 9];

% get diffs
ab = b - a;

% find number of steps required to be "one pixel wide" in the shorter
% two dimensions
n = max(abs(ab)) + 1;

% compute line
s = repmat(linspace(0, 1, n)', 1, 3);
for d = 1:3
    s(:, d) = s(:, d) * ab(d) + a(d);
end

% round to nearest pixel
s = round(s);

% if desired, apply to a matrix
N = 10;
X = zeros(N, N, N);
X(sub2ind(size(X), s(:, 1), s(:, 2), s(:, 3))) = 1;

% or, plot
clf
plot3(s(:, 1), s(:, 2), s(:, 3), 'r.-')
axis(N * [0 1 0 1 0 1])
grid on

请原谅丑陋的代码,我这么做很匆忙;)

三维布雷森汉姆?是的,看来我的答案是Bresenham的线路实现。毫不奇怪,我想到了辛克莱的老谱图;)。