The Code to replace non-threshold indexes in Array

10 ビュー (過去 30 日間)
DB
DB 2024 年 10 月 14 日
回答済み: Steven Lord 2024 年 10 月 14 日
Hello,
A = randi(5,5);
idx = find(A > 3);
A(idx) = log10(A(idx));
After running the above lines of code, what's the easiest(fastest) way to replace the non idx index in A with a certain value(100 for example)?
A(~idx) = 100; doesn't work the way I intended to.
Thanks!

採用された回答

Steven Lord
Steven Lord 2024 年 10 月 14 日
Get rid of the find call.
A = randi(5,5)
A = 5×5
5 3 2 2 5 1 1 4 5 3 1 4 2 5 2 1 2 3 2 2 1 1 2 5 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
idx = (A > 3) % Make a logical mask
idx = 5x5 logical array
1 0 0 0 1 0 0 1 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0
A(idx) = log10(A(idx)) % Use the logical mask to identify locations to change
A = 5×5
0.6990 3.0000 2.0000 2.0000 0.6990 1.0000 1.0000 0.6021 0.6990 3.0000 1.0000 0.6021 2.0000 0.6990 2.0000 1.0000 2.0000 3.0000 2.0000 2.0000 1.0000 1.0000 2.0000 0.6990 3.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A(~idx) = 100 % Use the negation of the logical mask to identify locations to change
A = 5×5
0.6990 100.0000 100.0000 100.0000 0.6990 100.0000 100.0000 0.6021 0.6990 100.0000 100.0000 0.6021 100.0000 0.6990 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 100.0000 0.6990 100.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by