Simple Question (I think) about finding zeros(and other number) in an array
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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]
Accepted Answer
madhan ravi
on 7 Sep 2018
Edited: madhan ravi
on 7 Sep 2018
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 Comments
madhan ravi
on 7 Sep 2018
If it works please do accept my answer.
Manne Plok
on 7 Sep 2018
Hi, what if I wanted to change two things whenever Q == P, eg I wanted to change Q values to 6 and increase count by 1?
madhan ravi
on 7 Sep 2018
Can you give an example and your required output ?
madhan ravi
on 7 Sep 2018
Then you should be using loop.
for i=1:length(Q)
if Q(i)==P(i)
Q(i) = 6+1
else
continue
end end
Image Analyst
on 7 Sep 2018
Count? Count of what?
madhan ravi
on 7 Sep 2018
But give an example so that we could meet your requirements.
Manne Plok
on 7 Sep 2018
Edited: Manne Plok
on 7 Sep 2018
I have two other arrays C and D.
C = [ 3 4 5 6 7 8]
R = [ 1 2 0 1 0 2]
D = [ 8 7 6 5 4 3]
if R == 0
R = 3
C = C - 1
D = 1
end
So whenever R = 0, that same position in C should lower by 1 too. Also that same position in D should be 1.
Intended output:
R = [1 2 3 1 3 2]
C = [3 4 4 6 6 8]
D = [8 7 1 5 1 3]
madhan ravi
on 7 Sep 2018
Edited: madhan ravi
on 7 Sep 2018
C = [ 3 4 5 6 7 8]
r = [ 1 2 0 1 0 2]
D = [ 8 7 6 5 4 3]
for i=1:numel(r)
if r(i) == 0
r(i) = 3
C(i) = C(i) - 1
D(i) = 1
else continue
end
end
Manne Plok
on 7 Sep 2018
Yes this works! I was trying to avoid for loops (for speed) but I guess you cant avoid them forever :(
madhan ravi
on 7 Sep 2018
Edited: madhan ravi
on 7 Sep 2018
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
madhan ravi
on 7 Sep 2018
Give a vote if you find the latter without loop useful.
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)