I have a vector a = [1 5 3 4 2]
. I'd like to find all elements of a, which are 1<a<5
. How do I do it in Matlab?
Personally I've developed one solution, but it's cumbersome:
a = [1 5 3 4 2];
ix = find(a>1);
ix = ix(find(a(ix)<5));
disp(a(ix))
What's a better way?
Use logical indexing:
>> a = [1 5 3 4 2];
>> a = a(1 < a & a < 5)
a =
3 4 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With