フィルターのクリア

Substitute values in an array

1 回表示 (過去 30 日間)
Inês Mendes
Inês Mendes 2015 年 11 月 30 日
編集済み: Stephen23 2015 年 11 月 30 日
Hi guys,
I have a question.
I have an array like this:
d=[1 1 1 1 1 2 2 2 2 2 1 1 1 1 1 ]
How can i substitute the values of the array knowing that for the first 5 positions i want do to d=6 and for the last 4positions i want do do d=8? This means i can´t use d(d==1)= 6 because the values are different for each position...
Can anyone help?
Thanks in advance!
Inês
  1 件のコメント
Stefan Raab
Stefan Raab 2015 年 11 月 30 日
編集済み: Stefan Raab 2015 年 11 月 30 日
Hey, I am not sure if I get you right, but this might solve your problem:
d(1:5) = 6;
d((end-3):end) = 8;

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

回答 (1 件)

Stephen23
Stephen23 2015 年 11 月 30 日
編集済み: Stephen23 2015 年 11 月 30 日
If you do not know the indices in advance then you can use diff and find:
>> d = [1,1,1,1,1,2,2,2,2,2,1,1,1,1,1];
>> f = find(diff(d));
>> d(1:f(1)) = 6;
>> d(1+f(end):end) = 8
d =
6 6 6 6 6 2 2 2 2 2 8 8 8 8 8
Or if the indices are known then they can be hardcoded:
>> d = [1,1,1,1,1,2,2,2,2,2,1,1,1,1,1];
>> d(1:5) = 6;
>> d(11:15) = 8
d =
6 6 6 6 6 2 2 2 2 2 8 8 8 8 8

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by