필터 지우기
필터 지우기

Replacing numbers in a row

조회 수: 1 (최근 30일)
John Doe
John Doe 2018년 4월 17일
댓글: John Doe 2018년 4월 19일
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월 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일

Just the 3 that stands alone

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

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 4월 18일
A( strfind(A, [0 3 0]) + 1) = 0;

Community Treasure Hunt

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

Start Hunting!

Translated by