MATLAB Answers

0

How to replace all odd numbers with infinity using find()

Asked by timetry2 on 11 Oct 2019
Latest activity Commented on by Adam Danz
on 12 Oct 2019 at 23:21
I have a code that looks like this: C = randi([100,200],1000,1000);
How would I replace all the odd numbers with infinity using the find() function. I read up on find but I am unsure how to replace just the odd numbers.
Thank you in advance

1 Answer

Answer by Walter Roberson
on 11 Oct 2019
 Accepted Answer

locations = find( Value_Is_Odd(C) );
where Value_Is_Odd is a function or expression that you write that takes in an array of values and returns a logical array of which entries are odd.
Hint: rem(), mod()

  11 Comments

Thank you so much for your help
I ended up with a code that looks like this:
C = randi([100,200],1000,1000);
evenIndices = rem(C,2) == 0
allTheEvenNumbers = C(evenIndices)
allTheEvenNumbers = allTheEvenNumbers -1
locations = find(allTheEvenNumbers);
C(locations) = Inf
However, the odd numbers are not being replaced by Inf. What am I doing wrong?

Sign in to comment.