Index of the first particular value in a vector

3 ビュー (過去 30 日間)
Garrett Duckworth
Garrett Duckworth 2018 年 3 月 16 日
回答済み: Walter Roberson 2018 年 3 月 16 日
How should I go about finding the index for the first value in a given vector for a given value. For an example, if I have some vector x = [9, 14, 13, 14], and I want to find the index for the first value in it that is 14, in this case 2, to replace it with a different value, say 1, while letting the subsequent values remain unchanged, to get a final answer of x = [9, 1, 13, 14].
I have thought about simplifying the problem by changing it to a Boolean version, c = x == 14, so c becomes [0, 1, 0, 1] however I still the issue of finding only the first value to change.
I know I can probably just run the whole thing through a loop through each subsequent entry in x until I find the first value but surely there is a more elegant and compact way.

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 3 月 16 日
find(x == 14, 1)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by