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

조회 수: 3 (최근 30일)
timetry2
timetry2 2019년 10월 11일
댓글: Adam Danz 2019년 10월 12일
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에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by