Find the index of given value in an array

698 ビュー (過去 30 日間)
Mirlan Karimov
Mirlan Karimov 2019 年 4 月 10 日
コメント済み: Anoop M 2021 年 1 月 12 日
array = [ 1 2 3 4 5 6 ];
find(array == 3);
This is clear!
I want to find fractional index when array == 2.5 or any other intermediate value.
  3 件のコメント
madhan ravi
madhan ravi 2019 年 4 月 10 日
When you ask a question , make sure you give an example clearly instead of advising others how to answer the question.
Anoop M
Anoop M 2021 年 1 月 12 日
If you bother to write a comment, you can write the answer instead of advising on how to write a question.

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

採用された回答

Stephen23
Stephen23 2019 年 4 月 11 日
編集済み: Stephen23 2019 年 4 月 11 日
Much simpler (and also works for multiple val values):
interp1(array,1:numel(array),val)
For example:
>> array = [2,4,5,7,8,9]; % a more interesting sequence.
>> val = 3.8;
>> interp1(array,1:numel(array),val)
ans = 1.9
And compared to the (very complex) accepted answer:
>> idxAboveVal = find( array >= val, 1 );
>> idxFract = idxAboveVal - ( array( idxAboveVal ) - val ) / ( array( idxAboveVal ) - array( idxAboveVal - 1 ) )
idxFract = 1.9
Note: this answer is based on the original answer by madhan ravi:
  1 件のコメント
Adam
Adam 2019 年 4 月 12 日
Yeah, I was originally going to give an answer based on interp1 but had a brainfade on working out how to use it for this!

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

その他の回答 (3 件)

Hayden Birch
Hayden Birch 2020 年 11 月 17 日
Whenever I've wanted to find the index of a specific value I subtract the value of the element I want then take the min() of the abs() of that.
Array = [2,4,5,7,8,9,11,0,3.8,3,7,13]
TargetValue = 3.8;
[ZERO,i] = min(abs(Array - TargetValue))

Adam
Adam 2019 年 4 月 10 日
編集済み: Adam 2019 年 4 月 10 日
val = 2.5;
idxAboveVal = find( array >= val, 1 );
idxFract = idxAboveVal - ( array( idxAboveVal ) - val ) / ( array( idxAboveVal ) - array( idxAboveVal - 1 ) );
I'm guessing this is what you mean.
Obviously it would need error checking if idxAboveVal is 1 or empty.
There's probably neater ways to do it too, or shorter, at least!
  3 件のコメント
Adam
Adam 2019 年 4 月 11 日
The find function simply finds integer indices into an array that correspond to the logical expression you give it. It isn't magic. It can't find things that don't exist. Hence I used it to find the next value greater than the one you want and did the required maths from there.
You should always give an example that shows the full complexity of the question you are asking though if you want a useful answer.
Giving what people call a 'Minimum working example' is fine, but it needs to have the full complexity of what you actually want to know still, otherwise it's of no use.
A manual approach to things causes bugs if you get it wrong, not if you get it right.
Mirlan Karimov
Mirlan Karimov 2019 年 4 月 11 日
I had a manual code but then realized yours is shorter and I will be using that one. Thanks!

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


Leonardo Alvarez
Leonardo Alvarez 2020 年 1 月 22 日
hello
I have two series both with 52560x1 size. One is temperature and the other is time both start at 2017,1,1,00,00,00 and end at 2017,12,31,23,50,00 with a 10 minutes sample.
I want to extract both temperature and time for elements starting at 2017,4,15,00,00,00 and ending at 2017,4,30,23,50,00
Can anyone give me a hand on it
Thanks in advance

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by