在MATLAB中选择矩阵的对角元素 [英] Select Diagonal Elements of a Matrix in MATLAB
问题描述
在MATLAB中考虑以下矩阵:
Consider the following matrix in MATLAB:
01 02 03 04 05 06 07
01 02 03 04 05 06 07
08 09 10 11 12 13 14
08 09 10 11 12 13 14
15 16 17 18 19 20 21
15 16 17 18 19 20 21
22 23 24 25 26 27 28
22 23 24 25 26 27 28
29 30 31 32 33 34 35
29 30 31 32 33 34 35
36 37 38 39 40 41 42
36 37 38 39 40 41 42
43 44 45 46 47 48 49
43 44 45 46 47 48 49
我必须为图像的7 x 7窗口(移动)生成方向变异图。我将使用 nlfilter
进行处理,但是为了开发函数来计算变量图,我无法决定如何在窗口中选择元素。例如,当我考虑中心值25时,在EW方向上我只需要考虑25,26,27和28;在NE方向上,当选择的滞后时间为1时,我只需要考虑25,19,13和07.是否有任何标准命令可以这样做?
I have to generate directional variograms for such 7 x 7 windows(moving) of an image. I will use nlfilter
for the process but for developing the function to calculate variograms I am not able to decide how to select elements in the window. For example when I consider the central value 25, in EW direction I have to consider only 25, 26, 27 and 28; in NE direction I have to consider only 25, 19, 13 and 07 when the lag chosen is 1. Is there any standard command to do so?
推荐答案
您可以编写一个函数来轻松获取这些元素:
You can write a function to get these elements yourself easily:
A = [01 02 03 04 05 06 07
08 09 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
43 44 45 46 47 48 49];
c = (size(A)+1)/2;
EW = A(c(1),c(2):end)
NE = diag(A(c(1):-1:1,c(2):end))
只需在函数(最好是m文件)中编写此代码,执行操作并将结果传回。
Just write this code in a function (preferably an m-file), perform your operation and pass the result back.
diag
函数返回矩阵的对角元素(或传递向量时返回对角矩阵) 。
The diag
function returns the diagonal elements of a matrix (or returns a diagonal matrix when passed a vector).
这篇关于在MATLAB中选择矩阵的对角元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!