check the neighbours of a cell in a vector Matlab -
i have 2 vectors
k=[1 1 1 2 1 2 1 4 2 10 4 5 1]
and
l=[2 0 1 2 1 2 1 3 2 0 1 2 1]
i want compare value of 7th element in each vector neighbours of value, neighbours 5 elements next element in each side. k
, 7th element 1
, neighbours 1 1 1 2 1 2
(left neighbours) , 4 2 10 4 5 1
(right neighbours).
for l
, 7th element 1
, neighbours 2 0 1 2 1 2
(left neighbours) , 3 2 0 1 2 1
(right neighbours). if difference between 7th value , each of neighbours above threshold i'll e.g x=1, if not i'll thing e.g x=2
.
so in example i'll set threshold 3, k 7th element value 1 , difference between , 2 of neighbours 10,5 more threshold value 3 x 1. l 5th element value 1 , difference between , of neighbours less threshold value 3 x 2. i'm wondering if assist me condition, i'm not sure if can done without loops save time.
you can check condition using any
, or
:
n = 5; % reference index t = 3; % threshold v = l; % used pass vector l if-statement % v = k; % formulate if-statement check values % below/above index n , check if difference % exceeds threshold % or-statement (because not matter if % threshold exceeded above index n or below) % expressed | if any((v(1:n-1)-v(n))>t) | any((v(n+1:end)-v(n))>t) x = 1; else x = 2; end
note
depending on matlab version v(1:n-1)-v(n)
not work, because matrix dimensions not agree. in case use: v(1:n-1)-ones(size(v(1:n-1))).*v(n)
Comments
Post a Comment