Matlab: Matrix with negative numbers

75 ビュー (過去 30 日間)
Articat
Articat 2019 年 6 月 10 日
コメント済み: madhan ravi 2019 年 6 月 10 日
0
I have a matrix, A = [745 x 678], which consists of negative and positive values. I would like to make all the negative numbers zero but am not sure how to go about doing this.
I was thinking of something like:
n = 678
for i = 1:n
A(A<0) = 0
end
I am not sure to how to apply this to every element of the matrix though. Any help would be appreciated.

採用された回答

madhan ravi
madhan ravi 2019 年 6 月 10 日
No loops needed:
matrix(matrix <0) = 0;

その他の回答 (1 件)

pankhuri kasliwal
pankhuri kasliwal 2019 年 6 月 10 日
Hi,
you do not need to iterate over all the elements of a matrix to do so, you can simply do this:
m = [1 2 -1; -2 4 -7; 9 -5 1];
m(m<0) = 0 ;
this will give you
m = [1 2 0; 0 4 0; 9 0 1]
  1 件のコメント
madhan ravi
madhan ravi 2019 年 6 月 10 日
The same answer was given 3 hours ago.

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

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by