フィルターのクリア

How to replace a certain value for another one once in a vector without using the index?

1 回表示 (過去 30 日間)
vec = [89.7000 9.1000 68.0000 87.7000 91.7000 9.1000]
rep = 50
I have to replace one of the 9.1000s in vec for rep without using indices.
  1 件のコメント
Star Strider
Star Strider 2017 年 2 月 5 日
I have to replace one of the 9.1000s in vec for rep without using indices.
That does not seem possible to me. I cannot imagine a way to address the elements of a vector without using some sort of indexing.

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

採用された回答

Sebastian
Sebastian 2017 年 2 月 5 日
編集済み: Sebastian 2017 年 2 月 5 日
Use this to replace all elements with the value of 9.1 with rep:
vec(vec == 9.1) = rep
vec =
89.7000 50.0000 68.0000 87.7000 91.7000 50.0000
Alternatively use this to find the indices of the elements and then replace them:
ind = find(vec == 9.1)
ind =
2 6
vec(ind) = rep
vec =
89.7000 50.0000 68.0000 87.7000 91.7000 50.0000
If you only want to replace the first element, use this:
vec(find(vec == 9.1, 1)) = rep
vec =
89.7000 50.0000 68.0000 87.7000 91.7000 9.1000
  2 件のコメント
Star Strider
Star Strider 2017 年 2 月 5 日
That is implied indexing, since find returns the index.
Sebastian
Sebastian 2017 年 2 月 5 日
編集済み: Sebastian 2017 年 2 月 5 日
I know but I think there is no way around find if you want to replace only certain elements and not all of them.

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

その他の回答 (0 件)

カテゴリ

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