Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.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-在3d矩阵中查找2d矩阵值_Matlab_Matrix_Compare - Fatal编程技术网

Matlab-在3d矩阵中查找2d矩阵值

Matlab-在3d矩阵中查找2d矩阵值,matlab,matrix,compare,Matlab,Matrix,Compare,我有以下问题。我有两个矩阵,一个是尺寸为X,Y的2d矩阵,其中一组地形高度取自DEM文件,另一个是尺寸为X,Y,Z的3d矩阵,每个(X,Y)点的Z高度值为0到5000米 我想将每个(X,Y)点的DEM高度与Z高度值列进行比较,并取最接近的一个。例如: dem(1,1) = 1850 %actual height of the terrain at point (1,1) heights(1,1,:) = 0, 1000, 2000, 3000, 4000, 5000 %column of hei

我有以下问题。我有两个矩阵,一个是尺寸为X,Y的2d矩阵,其中一组地形高度取自DEM文件,另一个是尺寸为X,Y,Z的3d矩阵,每个(X,Y)点的Z高度值为0到5000米

我想将每个(X,Y)点的DEM高度与Z高度值列进行比较,并取最接近的一个。例如:

dem(1,1) = 1850 %actual height of the terrain at point (1,1)
heights(1,1,:) = 0, 1000, 2000, 3000, 4000, 5000 %column of heights at point (1,1)
如果使用函数“find”,则会出现以下错误:

find(heights > dem, 1)
Error using  > 
Number of array dimensions must match for binary array op.
有没有不需要两个for循环的解决方案


非常感谢您的帮助

您只需将数据定义为:

dem(1,1) = 1850;
heights(1,1,:) = [0; 1000; 2000; 3000; 4000; 5000];
现在,
find(heights>dem,1)
yeilds

ans =

     3

这是预期的结果,
2000

的索引您可以使用
bsxfun
将其简化为单个维度上的循环:

heights = rand(10, 10, 10);
dem = rand(5, 1);
bsxfun(@gt, heights(1, :, :), dem)

    [returns a 5x10x10 matrix]

如果
X
Y
保持不变,那么问题不就归结为n
Z
数组之间的比较吗?因为我希望每个点的坐标在2d和3d表示之间是不变的。我说的对吗?是的,对于每个点,它是长度为Z的向量和一个值之间的比较。可以使用
find(高度(x,y,:)>dem(x,y))
为每个点执行此操作。但是,我希望同时为整个矩阵提供一个解决方案,而不在X和Y坐标之间循环。在这个示例中,我只给出了矩阵的一个点的值。我的目标是同时完成(X,Y)点的整组操作。非常感谢!我刚才在看这个函数:
result=bsxfun(@ge,heights,dem)它的工作原理与预期一样。请注意,您仍在循环!不,你不是,除了在单一维度上,
bsxfun
无法处理
bsxfun
执行
repmat
操作,使一维数据与多维数据的大小相同,然后立即执行整个操作。