convert negative zero of matrix into zero

14 visualizaciones (últimos 30 días)
Suma
Suma el 13 de Sept. de 2014
Respondida: Rick el 31 de Oct. de 2019
if true
l(:,k) = eig(A(:,:,k));
like this,
l =
Columns 1 through 5
-0.0000
1.0000
end
i want to make the -0.0000 to 0, how can i do this?
i tried
l(l < 0 )=0
but i don't know if it working because i still get errors
i also tried
if true
l(:,k) = eig(A(:,:,k));
l(:,k)(l(:,k) < 0 )
end
i get cannot call or index into a temporary array
thanks

Respuestas (2)

Geoff Hayes
Geoff Hayes el 13 de Sept. de 2014
Suma - what do you mean by you tried l(l < 0 )=0 but i don't know if it working because i still get errors? What errors are you observing, or is it that it just didn't work? Trying something like that line of code will set all negative elements of the matrix to zero, not just those that are close to zero (and still negative).
An alternative is to define a tolerance value and write some code that "says" something like "if the absolute value of the element is very close to my tolerance, then consider this element to be zero". You could do the following
A = magic(4);
A(1,1) = A(1,1)/-50000000;
A(2,2) = A(2,2)/-50000000;
A(3,3) = A(3,3)/-50000000;
A(4,4) = A(4,4)/-50000000;
A(1,4) = A(1,4)/50000000;
The matrix A has elements along the diagonal that are negative and close to zero
A =
-0.0000 2.0000 3.0000 0.0000
5.0000 -0.0000 10.0000 8.0000
9.0000 7.0000 -0.0000 12.0000
4.0000 14.0000 15.0000 -0.0000
Now define a tolerance value such as
tol = 1.e-6;
Now do the following
A(A<0 & A>-tol) = 0;
All negative elements of A (first condition) which are greater than the negative of the tolerance (second condition) should be set to zero. And so A becomes
A =
0 2.0000 3.0000 0.0000
5.0000 0 10.0000 8.0000
9.0000 7.0000 0 12.0000
4.0000 14.0000 15.0000 0
Note how only the elements along the diagonal are affected. The small but positive element at A(1,4) remains as is.

Rick
Rick el 31 de Oct. de 2019
Use this code. Matlab supports -0 as a value, but will show true if -0==0 is tested. This will set all zero entries to positive zero:
A(A==0)=0;

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by