1

我有以下问题。我有两个矩阵,一个大小为 X,Y 的 2d 矩阵具有一组从 DEM 文件中获取的地形高度,以及一个大小为 X,Y,Z 的 3d 矩阵,每个矩阵的 Z 高度值从 0 到 5000 米(X,Y ) 观点。

我想将每个 (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(heights > dem, 1)
Error using  > 
Number of array dimensions must match for binary array op.

有没有不需要两个for循环的解决方案?

非常感谢您的帮助!

4

2 回答 2

2

您可以使用以下方法将其减少为单个维度上的循环bsxfun

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

    [returns a 5x10x10 matrix]
于 2013-04-04T11:00:37.927 回答
0

您只需将数据定义为:

dem(1,1) = 1850;
heights(1,1,:) = [0; 1000; 2000; 3000; 4000; 5000];

现在,find(heights > dem, 1)收成

ans =

     3

这是预期的结果, 的索引2000

于 2013-04-04T10:34:21.083 回答