我有两个向量
K=[1 1 1 2 1 2 1 4 2 10 4 5 1] 和
L=[2 0 1 2 1 2 1 3 2 0 1 2 1]我想将每个向量中的第7个元素的值与这个值的邻域进行比较,其中相邻的5个元素在每个边的元素旁边。因此,对于K,第七个元素是1,而邻居是1 1 1 2 1 2 (左邻居)和4 2 10 4 5 1 (右邻居)。
对于L,第七个元素是1,而邻居是2 0 1 2 1 2 (左邻居)和3 2 0 1 2 1 (右邻居)。如果7值与其每个邻居之间的差值超过了某一阈值,那么我将做一些事情,例如X=1,如果不是,我将做另一件事情,例如X=2。
所以在我的例子中,我将阈值设置为3,对于K,第7元素值是1,它和它的两个邻居10,5之间的差异大于阈值3,所以X将是1。对于L,第5元素值是1,它和它的所有邻居之间的差值小于阈值3,所以X将是2。所以我想知道是否有人能帮助我完成这个条件,我不确定这是否可以通过循环来节省时间。
发布于 2013-08-15 17:50:15
可以使用any和or检查此条件:
N = 5; % reference index
T = 3; % threshold
V = L; % used to pass the vector L to the if-statement
% V = K;
% formulate if-statement to check for values
% below/above index N and check if any difference
% exceeds the threshold
% the or-statement (because it does not matter if the
% threshold is exceeded above index N or below)
% is expressed as |
if any((V(1:N-1)-V(N))>T) | any((V(N+1:end)-V(N))>T)
X = 1;
else
X = 2;
end备注
取决于您的Matlab版本,V(1:N-1)-V(N)将无法工作,因为矩阵尺寸不一致。在本例中使用:V(1:N-1)-ones(size(V(1:N-1))).*V(N)
https://stackoverflow.com/questions/18257161
复制相似问题