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 件のコメント
Adam Danz
2019 年 10 月 11 日
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 件のコメント
timetry2
2019 年 10 月 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
timetry2
2019 年 10 月 11 日
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
timetry2
2019 年 10 月 12 日
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?
Walter Roberson
2019 年 10 月 12 日
Hint: rem(), mod()
timetry2
2019 年 10 月 12 日
Thank you so much for your help
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?
Adam Danz
2019 年 10 月 12 日
その他の回答 (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!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
