Placing a number to regularly increasing array

2 ビュー (過去 30 日間)
Orhan Celikkaya
Orhan Celikkaya 2018 年 1 月 5 日
編集済み: Stephen23 2019 年 6 月 21 日
Hi everyone,
I have very basic problem but I can't deal with it. I have an array that regulary increasing e.g. 1,2,3,4,5..,10. I want to write a function that take decimal or integer number as input and determines that this number's place in that array. For example I wrote 3.2 as input and the function that I'll write should determine this number is between 3 and 4. Is there any function do the same thing? If not, how can I solve this? Any thoughts?
Thanks in advance.

回答 (1 件)

Navdha Agarwal
Navdha Agarwal 2019 年 6 月 21 日
I hope the following snippet help you.
a = 1:10;
insert = 3.2;
for i = 1:length(a)
if( i == 1 && insert <= a(i)) % if the element to be inserted is smaller than the first element of the array
b = [insert a];
break;
elseif( insert >= a(i) && insert <=a(i+1)) % if the element to be inserted is in between the array
b = [a(1:i) insert a(i+1:end)];
break;
else % if the element to be inserted is greater than all the elements in the array and is inserted at the end
b = [a insert];
break;
end
end
disp(b)
  1 件のコメント
Stephen23
Stephen23 2019 年 6 月 21 日
編集済み: Stephen23 2019 年 6 月 21 日

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by