Dear all,
I have a question, I want to try to replace values in a row that are greater than zero. For example, I have the array A that is:
A = [ 0 0 3 5 6 4 0 0 0 3 0 0 3 2 0].
What I want is to replace all values greater than 0 by 1.I know I can use A(A>0)=1 but I don't want to replace the individual 3 by 1 instead I want it to be zero.
I want my result to be:
A = [ 0 0 1 1 1 1 0 0 0 0 0 0 1 1 0]....instead of A = [ 0 0 1 1 1 1 0 0 0 1 0 0 1 1 0].
Is there any way I can do this and specify what individual number I don't want to replace by 1??
Thank You!!

6 件のコメント

Guillaume
Guillaume 2018 年 4 月 17 日
Looks like you're still replacing the first 3. Is that correct?
James Tursa
James Tursa 2018 年 4 月 17 日
So, can you tell us the explicit rule(s) for the replacement instead of us having to guess at it?
Bosong Lin
Bosong Lin 2018 年 4 月 17 日
I guess you are looking for a pattern like in A, '0 3 0', you would like to replace this '3' by '0' also.
John Doe
John Doe 2018 年 4 月 18 日
I Bosong Lin is right...The number 3 that stands alone in 0 3 0 pattern..I would like to replace by 0 otherwise it would be 1 like the rest of the values
Guillaume
Guillaume 2018 年 4 月 18 日
So is it just the 3 that stand alone that are not to be replaced, or any number that stand alone?
John Doe
John Doe 2018 年 4 月 18 日
Just the 3 that stands alone

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

 採用された回答

Matt J
Matt J 2018 年 4 月 18 日
編集済み: Matt J 2018 年 4 月 18 日

0 投票

Just the 3 that stands alone

result = strrep(A,[0,3,0],[0,0,0])>0;

1 件のコメント

John Doe
John Doe 2018 年 4 月 19 日
Thank you!!

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 4 月 18 日

0 投票

A( strfind(A, [0 3 0]) + 1) = 0;

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

質問済み:

2018 年 4 月 17 日

コメント済み:

2018 年 4 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by