Determine set of data between multiple ranges from a matrix
조회 수: 6 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Guillaume
2017년 2월 9일
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))
추가 답변 (1개)
Rik
2017년 2월 9일
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
Rik
2017년 2월 9일
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
2017년 2월 9일
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).
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!