How to find the element of a number if that number were to be placed in an ordered list?
2 ビュー (過去 30 日間)
古いコメントを表示
For example:
list = (0:10)
number= 2.5
the element it is in-between is 3 and 4
What is the most efficient way of finding where 2.5 would lie in that list and which elements it would be in-between?
Is there a better way than doing a for loop?
for i = 1:length(list)
if number > list(length(list))
fprintf('it is greater than any the numbers')
end
if number == list(i)
fprintf('it is element %d',i)
end
if number < list(length(list))
if number > list(i)
if number < list(i+1)
fprintf('it is between element %d and %d ',i,i+1 )
end
end
end
end
0 件のコメント
採用された回答
Stephen23
2020 年 2 月 22 日
The robust solution:
>> ida = find(list<number,1,'last')
ida = 3
>> idb = find(list>number,1,'first')
idb = 4
0 件のコメント
その他の回答 (1 件)
madhan ravi
2020 年 2 月 22 日
Between = [find(ismember(list1,fix(2.5))), find(ismember(list1,ceil(2.5)))]
5 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!