簡體   English   中英

Matlab-在3D矩陣中查找2D矩陣值

[英]Matlab - Find 2d matrix values in 3d matrix

我有以下問題。 我有兩個矩陣,一個是XD尺寸的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循環嗎?

預先非常感謝您的幫助!

您可以使用bsxfun將其減少到一個維度上的循環:

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

    [returns a 5x10x10 matrix]

您只需要將數據定義為:

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

現在, find(heights > dem, 1)

ans =

     3

這是預期的結果,索引為2000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM