Modifying the elements of a matrix using a condition

1 回表示 (過去 30 日間)
Rengin
Rengin 2014 年 10 月 7 日
コメント済み: Guillaume 2014 年 10 月 7 日
My matrix is Y=[1 2 5 0; 3 4 0 0; 7 0 3 0; 0 0 0 1]
I want to replace zeros '0' with '0.001'
How can I do it basically?
Thanks a lot!

回答 (1 件)

Guillaume
Guillaume 2014 年 10 月 7 日
編集済み: Guillaume 2014 年 10 月 7 日
As long as 0 is exactly 0 (and not some very small value that's close to zero):
Y(Y==0) = 0.001;
Probably better would be, if all Y>=0
Y(Y<0.001) = 0.001;
  2 件のコメント
Rengin
Rengin 2014 年 10 月 7 日
Could you also provide the loops for columns and rows, please?
Guillaume
Guillaume 2014 年 10 月 7 日
I don't understand what you're asking now. My answer will work whatever the shape and number of dimensions of Y.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by