How to replace negative elements in a Matrix with zeros?

A = [2, 3, -1, 5; -1, 4, -7, -3; -6, 0, 3, 9; 7, 6, -3, 8];
B = [9; 17; 15; -3];
AI = inv(A)
I = A*AI
X = AI*B
A*X
Now I am trying to set up a nested for loop to redefine negative elements in A. I need to replace negative elements in A with a zero. How do I go about doing this?

 Risposta accettata

Stephen23
Stephen23 il 17 Gen 2018
Modificato: Stephen23 il 20 Dic 2018
The simplest way is to use max:
A = max(A,0)
For example:
>> A = [2, 3, -1, 5; -1, 4, -7, -3; -6, 0, 3, 9; 7, 6, -3, 8]
A =
2 3 -1 5
-1 4 -7 -3
-6 0 3 9
7 6 -3 8
>> A = max(A,0)
A =
2 3 0 5
0 4 0 0
0 0 3 9
7 6 0 8

3 Commenti

Dear Mr. Cobeldick,
Your answer is very usefull and it helped me. So thank you for that!
But I have a question. I did not read in the documentation this syntax, nor I can understand it. Shouldn't
>> A = max(A,0)
produce
A =
2 3 -1 0
-1 0 -7 -3
-6 0 3 0
7 6 -3 0
(replace max number in every row) ?
Doing this:
B = max(A);
returns the maximum values along dim1.
On the other hand, doing this:
B = max(A,0)
is equivalent to doing
B = max(A,zeros(size(A)))
In these cases, we're comparing each element of A against 0 and picking the largest of the two values.
Stephen23
Stephen23 il 30 Apr 2021
Modificato: Stephen23 il 30 Apr 2021
Michael Seitaridis wrote: "I did not read in the documentation this syntax, nor I can understand it"
"Shouldn't A = max(A,0) produce ... "
The max documentation describes it as:
"C = max(A,B) returns an array with the largest elements taken from A or B."
What my answer shows is consistent with that explanation (given scalar expansion). Lets consider element A(1,4), which has value five. Can you explain why you think that the "largest" of zero and five should be zero? As far as I am aware, five is generally considered to be larger than zero.
"(replace max number in every row) ?"
I do not see that written anywhere in max the documentation.

Accedi per commentare.

Più risposte (2)

Or:
A(A < 0) = 0

3 Commenti

QuanCCC
QuanCCC il 19 Dic 2018
Modificato: Stephen23 il 20 Dic 2018
This worked perfectly
Jerzy Pela
Jerzy Pela il 27 Feb 2020
Modificato: Jerzy Pela il 27 Feb 2020
I compared both methods, since it was one of the bottlenecks in my calculations and max(A,0) was significantly faster. Keep it in mind if you need to do that calculation numerous times in your script. Otherwise both methods are equal
thank you for this extra little insight!

Accedi per commentare.

A = A*(A>0);
This also works!
Have a summary of possible methods:
A = A*(A>0);
A = max(A,0);
A(A<0) = 0;

2 Commenti

For non-scalar A (such as that shown in the question) the mtimes operator needs to be replaced with an element-wise times operator otherwise an error or incorrect output is quite likely:
A.*(A>0)
Also note that this method changes -Inf values to NaN, which may be an undesired side-effect:
>> A = [-1,0,1,;-Inf,Inf,NaN];
>> A = A.*(A>0)
A =
0 0 1
NaN Inf NaN
You'd need to multiple element-wise,
A = A.*(A>0);

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by