Determine set of data between multiple ranges from a matrix

8 visualizaciones (últimos 30 días)
Hi all,
I would just like to ask the following. Is there a function such that it lets you find the elements in a long vector between multiple, user specified ranges? e.g.
large_column_vector=[...];
upper_bounds=[20 23 45 69];
lower_bounds=[10 16 34 50];
new_large_column_vector=somefunction(upper_bounds,lower_bounds,large_column_vector);
So this function would take the large column vector, and find all elements between 10 and 20, 16 and 23, 34 and 45 etc.
Thanks for your help,
KMT

Respuesta aceptada

Guillaume
Guillaume el 9 de Feb. de 2017
It's hardly worth a function:
large_column_vector = randi(80, 1024, 1); %demo data
upper_bounds=[20 23 45 69];
lower_bounds=[10 16 34 50];
new_large_column_vector = large_column_vector(any(large_column_vector >= lower_bounds & large_column_vector <= upper_bounds, 2))
The above assumes R2016b, with its new implicit expansion.
In previous versions:
new_large_column_vector = large_column_vector(any(bsxfun(@ge, large_column_vector, lower_bounds) & bsxfun(@le, large_column_vector, upper_bounds), 2))

Más respuestas (1)

Rik
Rik el 9 de Feb. de 2017
There may be better solutions, but this code should do what you want. You can add in the equal signs if you need inclusive ranges.
function new_large_column_vector=somefunction(upper_bounds,lower_bounds,large_column_vector)
H=upper_bounds;L=lower_bounds;V=large_column_vector;%make shorthand
logic='';%initiate empty variable
for n=1:length(H)
logic=[logic '(V>' num2str(L(n)) '&V<' num2str(H(n)) ') | '];
end
logic=logic(1:(end-3));%remove extra ' | ' from the end
new_large_column_vector=eval(['V(' logic ')']);
end
  3 comentarios
Rik
Rik el 9 de Feb. de 2017
This was a quick and dirty approach, I'm aware of that. Your comment makes it sound like two separate problems, but if you want to use eval, you'll have to convert numeric indices to strings...
I don't get why people are so against the use of eval. It is just like a for-loop: try to avoid it if you can.
As a last note: I completely agree that your solution is better: shorter, more elegant and faster.
Guillaume
Guillaume el 9 de Feb. de 2017
We're against the use of eval because it encourages poor coding practices. You lose at lot by using eval:
  • tab completion
  • syntax highlighting
  • mlint autodetection of errors, warnings. A misspelling of a variable name, or invalid function use won't be detected by the code editor.
  • speed, because the JIT compiler has no way to know what happens inside eval it can't pre-compile or optimise the function code
  • automatic renaming of all instances of a variable/function, when you rename one instance. Won't work for the occurences inside eval.
  • clarity, it's much harder to understand code that has extra num2str, string concatenation, etc.
With eval, you're adding an extra layer on top of your code. This should be avoided at all cost unless there's absolutely no other way (which is extremely rare).

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by