smallest n such that vector(n) < a
2 次查看(过去 30 天)
显示 更早的评论
hi,
is there a efficient way that given a vector "vec" whose elements are monotonic increasing i can find the smallest index such that vec(n) < a. where "a" is some given value.
1 个评论
Image Analyst
2012-12-21
编辑:Image Analyst
2012-12-21
The smallest/lowest/first index will be the first one (index = 1) like Azzi said. But that is a trivial case and I think you actually mean the highest index, and that is what Wayne gave you code for. Like m = [11 12 13 14 15] and the value = 13.1. The lowest index where m < 13.1 is 1 and the highest is 3. Please confirm/clarify.
采纳的回答
Wayne King
2012-12-21
编辑:Wayne King
2012-12-21
x = 1:100;
a = 50;
[val,idx] = find(x<a,1,'last');
idx is the index of vec() and val is the value
If the values in the vector are monotonically increasing as you describe, then the 'last' option gives you the last index. Did you really mean you want the "smallest"?
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!