How to make all negative values in a matrix 0?
Mostra commenti meno recenti
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 Commento
Pleuni Kirch
il 18 Nov 2020
Risposta accettata
Più risposte (2)
Walter Roberson
il 18 Nov 2020
A(:, 2) = max(0, A(:, 2))
Andrew Flewellen-Gore
il 18 Nov 2020
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.
Categorie
Scopri di più su Creating and Concatenating Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!