Print a message if a condition is met

3 次查看(过去 30 天)
Max
Max 2012-11-28
Hi all, I have 8 ranges:
[ x(1) , x(2) ] , [ x(2) , x(3) ] ... [ x(7) , x(8) ]
memorized in a column-vector, x :
x = [x(1); x(2); ... ; x(8)]
and 7 values
p(1), p(2), ... , p(7)
I have to print only the values that are included in the corresponding range,
x(1) < p(1) & p(1) < x(2)
x(2) < p(2) & p(2) < x(3)
... so
x(1:end-1) < p(1:end) & p(1:end) < x(2:end)
With:
disp(find(~(x(1:end-1) < p(1:end) & p(1:end) < x(2:end))))
I print all the values that are outside of own range.
My goal is print also the values that are inside of own range. Thanks
Max
  1 个评论
Daniel Shub
Daniel Shub 2012-11-28
Maybe I am missing something but can't you just remove the ~?
disp(find((x(1:end-1) < p(1:end) & p(1:end) < x(2:end))))

请先登录,再进行评论。

回答(2 个)

Matt Fig
Matt Fig 2012-11-28
x = [1;2;3;4];
p = [1.5;3.5;3.2];
idx = x(1:end-1)<p & p<x(2:end);
SOL = p(idx)

Ilham Hardy
Ilham Hardy 2012-11-28
编辑:Ilham Hardy 2012-11-28
xa = [3;5;7;9;11;13;15;17];
ya = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];
res = ya(find(ya>xa(1)& ya<xa(end)&~ismember(ya,xa)));
disp(res)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by