How to replace all odd numbers with infinity using find()
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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
Please don't create duplicate questions
채택된 답변
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
But how would I replace the odd values with infinities?
locations would give you a vector list of index locations into C. You can assign something to all of those locations using the general forms
Array(Locations) = Value; %for scalar Value to be assigned to all those places
or
Array(Locations) = ValueVector; %for ValueVector being a vector the same size as Locations
I'm sorry Im still confused. So, could you show me what my code would look like? Thank you in advance
C is a matrix.
This line below will give you the row and column numbers that are TRUE in the find() inputs.
[rowNum, colNum] = find(. . .)
Or, as Walter explained, this version below gives you the linear index of locations.
locations = find(. . .)
Experiment with both versions to understand the outputs.
Look at this example:
A = 1:10
locations = [2 7 9]
A(locations) = -1
Further example:
A = 1:10;
locations = find(A > 5);
A(locations) = -1
I understand the code now with the example. Thank you so much. However, is there a way to only retrieve odd numbers without having to manually input them after find?
Hint: rem(), mod()
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?
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
