Matlab: Find points within a certain range in an array and their order/location in the array? -


i've array

x =  10 5 (e) 20 5 30 6 40 4 50 3 60 8 70 12 

and on...

i know value 5 i've called e. know located in array. want following:

  1. all elements in x(2:2:end) within range of +/-3 e. (which 5, 5, 6, 4, 3, 8).
  2. the corresponding x(1:2:end) values found within range. means final answer y should be:

     y =   10  5  20  5  30  6  40  4  50  3  60  8 

thanks lot!

in matlab/octave, can find indexes of non-zero elements function find. problem solved combining find logic operators:

y = x(find(x <= x(2)+3 & x >= x(2)-3)); 

explained:

e = x(2) x <= e+3   % produces matrix element-wise result (1 or 0). x >= e-3   % values determined logic operators >= , <=. find(x)    % returns matrix indeces of non-zero elements of x. x(find(x)) % returns non-zero elements. 

tested in octave (though should work in matlab too).


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -