# Simple Question (I think) about finding zeros(and other number) in an array

조회 수: 2 (최근 30일)
Manne Plok 2018년 9월 7일
댓글: madhan ravi 2018년 9월 7일
I have the following array.
R = [ 1 2 0 1 0 2]
if values in R = 0 I want to replace it with 3, so I did
If R == 0
R = 3
end
So it should end up
R = [ 1 2 3 1 3 2]
However as you probably know, this does not work. How do I make this work? Thanks! Same problem with the following:
P = [ 1 2 3 4 5]
Q = [ 5 4 3 2 5]
I want to find positions where P == Q so I tried
if P == Q
Q = 6
end
ie all positions where P = Q change to 6 so it should end up with
Q = [ 5 4 6 2 6]

댓글을 달려면 로그인하십시오.

### 채택된 답변

편집: madhan ravi 2018년 9월 7일
TRY THIS:
R = [ 1 2 0 1 0 2]
R(R==0)=3
P = [ 1 2 3 4 5]
Q = [ 5 4 3 2 5]
Q(Q==P)=6
##### 댓글 수: 11이전 댓글 9개 표시이전 댓글 9개 숨기기
편집: madhan ravi 2018년 9월 7일
C = [ 3 4 5 6 7 8]
R = [ 1 2 0 1 0 2]
D = [ 8 7 6 5 4 3]
D(R==0)=1
C(R==0)=C(R==0)-1
R(R==0)=3
Without loop
Give a vote if you find the latter without loop useful.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

R2017b

### Community Treasure Hunt

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

Start Hunting!

Translated by