How to make all negative values in a matrix 0?

34 ビュー (過去 30 日間)
Pleuni Kirch
Pleuni Kirch 2020 年 11 月 18 日
編集済み: cr 2020 年 11 月 19 日
for i= 1: size(A,1)
if A (i, 2) <= 0
DataNew(i,2) = 0
This is what I have now, but there are still negative values in my DataNew matrix
  1 件のコメント
Pleuni Kirch
Pleuni Kirch 2020 年 11 月 18 日
I am sorry I dont know how to change my question, but I only want to make the negative values of a certain column 0. Not all negative values in the matrix

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

採用された回答

cr
cr 2020 年 11 月 18 日
編集済み: cr 2020 年 11 月 19 日
%% Set all negative elements to 0
A(A<0) = 0;
%% Set second column elements to 0
col = 2;
a(a(:,col)<0,col) = 0;

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2020 年 11 月 18 日
A(:, 2) = max(0, A(:, 2))

Andrew Flewellen-Gore
Andrew Flewellen-Gore 2020 年 11 月 18 日
To set set negative values in a certain column of matrix "A" to 0, you can do this:
For this example we assume A is a 2-D number matrix and that we are getting rid of negative values in the 2nd column.
>>A(:,2) = max( A(:,2), 0 )
This line of code replaces each negative number in the second collumn with 0. Each positive number in the second collumn will stay the same.

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by