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

时间:2013-04-04 10:24:21

标签: matlab matrix compare

我有以下问题。我有两个矩阵,一个大小为X的二维矩阵,一个从DEM文件中获取的一组地形高度和一个大小为X,Y,Z的三维矩阵,每个的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循环?

非常感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

您可以使用bsxfun

将此缩小为单个维度的循环
heights = rand(10, 10, 10);
dem = rand(5, 1);
bsxfun(@gt, heights(1, :, :), dem)

    [returns a 5x10x10 matrix]

答案 1 :(得分:0)

您只需将数据定义为:

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

现在,find(heights > dem, 1)产生

ans =

     3

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

相关问题