Sorting and replacing data in matlab

1 回表示 (過去 30 日間)
Rubel Ahmed
Rubel Ahmed 2020 年 10 月 15 日
コメント済み: Rubel Ahmed 2020 年 10 月 16 日
Hi all,
I have a position Array suppose "A"
A =[ 1, 0, 3, 4, 5, 6, 7, 8, 0, 0,15, 20, 10, 9].
I want to replace all those values of A which are grater than 5.
Here, the first element (in A, it is 6) which is grater than 5 will be replaced by a constant say c1
Then next element (in A, it is 7) which is grater than 5 will be replaced by a constant say c2 =c1+ a, where 'a' is a constant
After that the next element (in A, it is 8) which is grater than 5 will be replaced by a constant say c3 =c2+ a, where 'a' is a constant
...... So on.
I want to see the final Array like this
A =[ 1, 0, 3, 4, 5, c1, c2, c3, 0, 0, c4, c5, c6, c7].
N.B. if any element of A is less or equal than 5, the elements of A will be unchanged

採用された回答

Ameer Hamza
Ameer Hamza 2020 年 10 月 15 日
編集済み: Ameer Hamza 2020 年 10 月 15 日
Here is a vectorized alternative:
A = [ 1, 0, 3, 4, 5, 6, 7, 8, 0, 0,15, 20, 10, 9];
a = 1; c1 = 10;
idx = A > 5;
A(idx) = c1 + (0:nnz(idx)-1)*a;
Result
>> A
A =
1 0 3 4 5 10 11 12 0 0 13 14 15 16
  1 件のコメント
Rubel Ahmed
Rubel Ahmed 2020 年 10 月 16 日

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

その他の回答 (1 件)

Alan Stevens
Alan Stevens 2020 年 10 月 15 日
Here's one way:
A =[ 1, 0, 3, 4, 5, 6, 7, 8, 0, 0,15, 20, 10, 9];
c1 = 0.1; a = 0.01;
ix = find(A>5);
for i = 1:numel(ix)
A(ix(i)) = c1+(i-1)*a;
end
  1 件のコメント
Rubel Ahmed
Rubel Ahmed 2020 年 10 月 16 日
Thanks Alan Stevens !!

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

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by