MATLAB,在更改每个步骤的矩阵中跟踪边界的最佳方法是什么? [英] MATLAB, what is the best way to trace a boarder in a matrix that is changing each step?
问题描述
我想在这样的矩阵中计算每次迭代的平均斜率或梯度。
I want to calculate the average slope or gradient at each iteration in such a matrix.
a=[ 10 9 8 7 6 5 5;
9 9 8 7 8 5 5;
8 8 7 7 5 5 5;
7 7 7 6 5 5 5;
6 6 6.6 5 5 5 5;
6 6 6.1 5 5 5 5;
6.3 5 5 5 5 5 5]
我想要找到斜坡或每个步骤中a(1,1)位置之间的梯度和每个值为5的每个点之间的梯度。每次迭代5的位置发生变化,其他值也发生变化。
Where I am wanting to find the slope or gradient between the a(1,1) position during each step and at each point that boarders a value of 5. Each iteration the position of the 5's changes and so do the other values.
这样做之后我将平均斜率。我还没有遇到这样的问题,我找不到简化的Matlab命令。
After doing so I will then average the slope. I haven't encountered a problem like this yet and I could not find a Matlab command to simplify.
推荐答案
首先你必须找到海岸元素是哪个。从你的定义来看,如果一个元素是一个海岸元素(如右边)和一个5.如果海平面是5,并且是最低可能的值,即没有元素超出海平面,那么你必须首先找到所有的土地要素为,
First you must find out which the coast elements are. From your definition, an element is a coast element if it border (from the right) with a 5. If the sea level is 5, and is the lowest possible value i.e. no element goes beyond sea level, then you must first find all the land elements as,
land=a>5;
返回,
ans =
1 1 1 1 1 0 0
1 1 1 1 1 0 0
1 1 1 1 0 0 0
1 1 1 1 0 0 0
1 1 1 0 0 0 0
1 1 1 0 0 0 0
1 0 0 0 0 0 0
现在,海岸元素是1,然后是0.取陆地矩阵的列差,
Now, the coast elements are 1s that are followed by a 0. Take the column difference of the land matrix,
coastTmp=diff(land,1,2);
返回,
ans =
0 0 0 0 -1 0
0 0 0 0 -1 0
0 0 0 -1 0 0
0 0 0 -1 0 0
0 0 -1 0 0 0
0 0 -1 0 0 0
-1 0 0 0 0 0
并找到-1s,
coast=find(coastTmp==-1);
这是,
coast =
7
19
20
24
25
29
30
从这里开始很简单。渐变是a(1,1)与所有海岸元素的差异,即
From here it is easy. The gradient is the difference of a(1,1) with all the coast elements, i.e.
slope=a(coast)-a(1,1); % negative slope here
给予,
slope =
-3.700000000000000
-3.400000000000000
-3.900000000000000
-3.000000000000000
-4.000000000000000
-4.000000000000000
-2.000000000000000
当然平均值是,
mean(slope);
这篇关于MATLAB,在更改每个步骤的矩阵中跟踪边界的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!