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

3 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2019-10-11
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 个评论
timetry2
timetry2 2019-10-12
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by