请帮助我用矩阵中的相邻元素检验我的假设
我无法弄清楚我研究的最后一部分,所以如果有人可以帮助我,我将非常感谢您的帮助..:)
假设我的原始矩阵是,
X =
0 0 0 0 0
0 0 12 9 0
0 4 9 15 0
0 11 19 0 0
0 2 4 8 0
0 4 5 8 0
0 0 0 0 0
在找到非零的平均值后,我会得到一些东西如下所示:
new_x =
0 0 0 0 0
0 0 **9.0000** 9.0000 0
0 4.0000 9.0000 **9.0000** 0
0 **8.3333** **8.0000** 0 0
0 2.0000 4.0000 8.0000 0
0 4.0000 5.0000 8.0000 0
0 0 0 0 0
请注意,任何大于 10 的元素都是“中心”,我们希望找到半径为 1 m 的非零元素的平均值。其中 1 米 = 距中心 1 个元素。
** ** 表示中心。
对于这一部分,我使用了以下内容(来自 gnovice) :
X=[0 0 0 0 0; 0 0 12 9 0; 0 4 9 15 0; 0 11 19 0 0;
0 2 4 8 0; 0 4 5 8 0; 0 0 0 0 0];
kernel=[0 1 0; 1 0 1; 0 1 0];
sumx=conv2(X,kernel,'same');
nx=conv2(double(X>0),kernel,'same');
index=(X>10);
new_x=X;
new_x(index)=sumx(index)./max(nx(index),1);
所以我的问题是我想将相邻元素与其中心进行比较它们是否相等、小于或大于。如果它大于或等于“1”,否则为“0”。此外,半径之外的任何元素都可以被忽略并替换为“0”。
例如,中间的9
在12、15、19个中心的半径范围内,所以取这些`min[9.000, 9.000, 8.000]的最小中心 ] = 8.000。 在这种情况下,4 将不被考虑,因为它不被称为“中心”以及最后两行中的 [ 8 4 5 和 8 ]。
所以我想要如下所示的内容:
Test_x =
0 0 0 0 0
0 0 1 1 0
0 0 1 1 0
0 1 1 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
我之前已经将第一部分放在论坛中,我非常感谢之前的每一个建议。
请给我一些开始的想法。我尝试过使用循环,但似乎效果不太好。任何可以为我完成这项工作的 MATLAB 函数。
非常感谢您的帮助。
MATLAB 初学者
我想我通过使用 Jonas 技术找到了这个问题的解决方案。感谢乔纳斯和诺维的帮助:)
<块引用>X=[0 0 0 0 0; 0 0 12 9 0; 0 4 9 15 0; 0 11 19 0 0; 0 2 4 8 0; 0 4 5 8 0; 0 0 0 0 0];
内核=[0 1 0; 1 0 1; 0 1 0];
sumx=conv2(X,内核,'相同');
nx=conv2(double(X>0),kernel,'相同');
avg_x=X;
avg_x(avg_x<10)=0;
索引=(avg_x>10);
avg_x(索引)=sumx(索引)./max(nx(索引),1);
平均 x =
0 0 0 0 0
0 0 9.0000 0 0
0 0 0 9.0000 0
0 8.3333 8.0000 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
<块引用>tmp_x=avg_x;
maxVal=max(avg_x(:))+1;
tmp_x(tmp_x==0)=maxVal;
tmp_x=imerode(tmp_x,内核);
Test_x=X>=tmp_x;
I could not figure out the last part of my research so if anyone could help me I would be really appreciated for the help.. :)
Say that my original matrix is,
X =
0 0 0 0 0
0 0 12 9 0
0 4 9 15 0
0 11 19 0 0
0 2 4 8 0
0 4 5 8 0
0 0 0 0 0
and after finding the average of the non-zeros I will get something like below:
new_x =
0 0 0 0 0
0 0 **9.0000** 9.0000 0
0 4.0000 9.0000 **9.0000** 0
0 **8.3333** **8.0000** 0 0
0 2.0000 4.0000 8.0000 0
0 4.0000 5.0000 8.0000 0
0 0 0 0 0
Note that any elements that are greater than 10 are the 'center' and we want to find the average of the non-zeros with the radius of say 1 m. where 1 meter = 1 element away from the center.
** ** means the center.
For this part I have used the following (from gnovice):
X=[0 0 0 0 0; 0 0 12 9 0; 0 4 9 15 0; 0 11 19 0 0;
0 2 4 8 0; 0 4 5 8 0; 0 0 0 0 0];
kernel=[0 1 0; 1 0 1; 0 1 0];
sumx=conv2(X,kernel,'same');
nx=conv2(double(X>0),kernel,'same');
index=(X>10);
new_x=X;
new_x(index)=sumx(index)./max(nx(index),1);
So my question is that I want to compare the neighbor elements with its center whether they are equal, lesser, or greater. If it is greater or equal then '1' or else '0'.Also whatever elements that are outside the radius can be ignored and replaced with '0'.
For example, the 9
in the middle is within the radius of 12, 15, and 19 centers, so take the minimum center of those `min[9.000, 9.000, 8.000] = 8.000.
In this case 4 will not take into the consideration as it is not called the 'center' as well as [ 8 4 5 and 8 ] in the last two rows.
So I want something like below:
Test_x =
0 0 0 0 0
0 0 1 1 0
0 0 1 1 0
0 1 1 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
I have put this first part in the forum before and I am really appreciated for every suggestion earlier.
Please give me some ideas to start with. I have tried using a loop but it didnt seem to work very well. Any MATLAB function that can do the job for me..
Thank you so much for the help.
Beginner at MATLAB
I think I found the solution for this question by using Jonas techniques. Thank you for the help Jonas and gnovie:)
X=[0 0 0 0 0; 0 0 12 9 0; 0 4 9 15 0; 0 11 19 0 0; 0 2 4 8 0; 0 4 5 8 0; 0 0 0 0 0];
kernel=[0 1 0; 1 0 1; 0 1 0];
sumx=conv2(X,kernel,'same');
nx=conv2(double(X>0),kernel,'same');
avg_x=X;
avg_x(avg_x<10)=0;
index=(avg_x>10);
avg_x(index)=sumx(index)./max(nx(index),1);
avg_x =
0 0 0 0 0
0 0 9.0000 0 0
0 0 0 9.0000 0
0 8.3333 8.0000 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
tmp_x=avg_x;
maxVal=max(avg_x(:))+1;
tmp_x(tmp_x==0)=maxVal;
tmp_x=imerode(tmp_x,kernel);
Test_x=X>=tmp_x;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你想要做的是创建一个基于 new_x 的新数组,用其 4 个连接的邻居中的最小值替换每个元素。然后您可以将新数组与 new_x 进行比较。
这是一种执行此操作的方法(需要图像处理工具箱)
请注意,我比您得到的具有此逻辑的多一个。
I think what you want to do is create a new array based on new_x that replaces every element by the minimum of its 4-connected neighbours. Then you can compare the new array to new_x.
Here's a way to do this (requires the image processing toolbox)
Note that I get one more ones with this logic than you do.