How can I change all numbers in matrix to 1? except for 0
42 ビュー (過去 30 日間)
古いコメントを表示
How can I change all numbers in matrix to 1? except for 0
I want to make the matrix which has 1(all the numbers) or 0.
0 件のコメント
採用された回答
Voss
2022 年 12 月 3 日
編集済み: Voss
2022 年 12 月 3 日
One way:
M = randi(4,[5 5])-2 % a matrix with zero and non-zero elements
M(M ~= 0) = 1 % replace non-zero elements with ones
Another way:
M = randi(4,[5 5])-2 % a matrix with zero and non-zero elements
M(logical(M)) = 1 % replace non-zero elements with ones
0 件のコメント
その他の回答 (2 件)
Walter Roberson
2022 年 12 月 3 日
logical(inputMatrix)
However this will fail if the input includes nan
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Resizing and Reshaping Matrices についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!