重采样矩阵并还原为一个矩阵 [英] Resampling Matrix and restoring in one single Matrix

查看:172
本文介绍了重采样矩阵并还原为一个矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是这个论坛的新手,所以请多多包涵. 我已经在这个Matlab问题上工作了一段时间了:

I am new to this forum, so please bear with me. I have been working on this Matlab problem for a while now:

我有tif格式的数字高程模型(DEM)new_sub(x,y).因此,它是一个包含高度(z)的x x y矩阵.我希望以不同的分辨率重新采样此DEM的一部分,并将其恢复到另一个矩阵中.到目前为止,我一直在使用for循环来更改DEM不同区域的分辨率,然后将结果写入xyz文件:

I have a digital elevation model (DEM) new_sub(x,y) in tif format. So it is a x-by-y matrix containing heights (z). I wish to resample parts of this DEM in different resolutions and restore this in another matrix. So far I have been working with for loops to change the resolution of different areas of the DEM and then wrote the results to an xyz-file:

x y z 1 1 123 1 2 233 1 3 231 2 1 235 2 2 531 2 3 452

x y z 1 1 123 1 2 233 1 3 231 2 1 235 2 2 531 2 3 452

,依此类推. 这是代码:

and so forth. Here is the code:

xmax = size(new_sub,2);
ymax = size(new_sub,1);

for k=1:200 % y
    for l=1:xmax % x
        fprintf(fid, '%d %d %d \n',l,xmax+1-k,new_sub(k,l));
    end
end

% 1:4
for k=200/2+1:size(new_sub,1)/2
    for l=1:size(new_sub,2)/2
        fprintf(fid, '%d %d %d \n',l*2,ymax+2-k*2,new_sub(k*2,l*2));
    end
end

这确实有效,但似乎相当复杂.而且,它不允许我将重新采样的区域存储在Matlab中的单个矩阵中.

This does work, but seems to be rather complicated. Moreover, it does not allow me to store the resampled areas in a single matrix within Matlab.

是否有更有效的方法来对具有不同分辨率的Matrix的某些区域进行重采样,将它们写入包含所有重采样区域的新Matrix中,然后将其写入文件中?我当时在研究repmap,但是想不出一种聪明的使用方法!

Is there a more efficient way of resampling certain areas of a Matrix with different resolutions, writing them into a new Matrix containg all resampled areas and then writing it to a file? I was looking into repmap, but could not think of a clever way of using it!

非常感谢您的帮助!

THeo

推荐答案

要在Matlab中对矩阵重新采样:

To re-sample a matrix in Matlab:

例如矩阵M:

M = [1  2  3  4  5; 
     6  7  8  9  10; 
     11 12 13 14 15; 
     16 17 18 19 20; 
     21 22 23 24 25];

如果要在第n个像素上采样,就这么简单:

If we wanted to sample on every nth pixel, it is as simple as this:

m = M(1:n:end, 1:n:end)

对于n=2

m = 1  3  5
    11 13 15
    21 23 25

我建议您阅读建立索引matlab ,以及使用冒号运算符在matlab中创建矢量

I suggest you read up on indexing in matlab and also on using the colon operator to create vectors in matlab

现在,为了获得您提到的"xyz"格式,请首先使用 meshgrid 生成X和Y坐标的矩阵.

Now in order to get in the "x y z" format you mentioned, first use meshgrid to generate matrices of X and Y coordinates.

[X, Y] = meshgrid(1:n:size(M,1), 1:n:size(M,2))

注意,我使用nXY进行降采样.现在,您只需要展平三个矩阵并将它们组合:

notice I use n to downsample X and Y. Now you just need to flatten the three matrices and combine them:

final = [X(:), Y(:), m(:)]

最后要保存为文件,我建议您在Matlab命令提示符下键入help savehelp dlmwrite,并使用这些功能之一保存final

Finally to save as a file I suggest you type help save or help dlmwrite in the Matlab command promt and use either of those functions to save final

这篇关于重采样矩阵并还原为一个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆