how to check negative values are there or not in a matrix????

i have a matrix a=[0.2 0.1 -0.1 0 0.9], now i want to check whether there is any negative value if yes then those element i want to make zero.
plz plz help me....
a<0 makes all greater than as 1 and other 0. but i want to make only negative values zero and other as same as they are.

2 comentarios

u can use the 'sign' function.
An approach to show off (lol) -
a=[0.2 0.1 -0.1 0 0.9]
a = 1×5
0.2000 0.1000 -0.1000 0 0.9000
max(a, 0)
ans = 1×5
0.2000 0.1000 0 0 0.9000

Iniciar sesión para comentar.

 Respuesta aceptada

Mischa Kim
Mischa Kim el 11 de Jun. de 2014
suchismita, use
a(a<0) = 0;

3 comentarios

thank you so much....
@ misha kim after making the negative integer to zero in matrix i want to get back to the a as originally with negative integers ,how do i do it?
Thanks @Mischa Kim this syntax helped me to find if a vector contains both neg and pos value or not!!

Iniciar sesión para comentar.

Más respuestas (2)

Thomas Richner
Thomas Richner el 9 de Sept. de 2018
If you want to know if a matrix contains any negatives (but not to replace them) the use
contains_negative = any(a<0); % returns true or false
Anirban Naskar
Anirban Naskar el 11 de Jun. de 2014
Editada: Anirban Naskar el 11 de Jun. de 2014
Hi Suchismita,
You can use something like the following:
[m n]=size(a);
for i=1:m
for j=1:n
if a(i,j)<0
disp('contains negative element');
a(i,j)=0;
end
end
end

2 comentarios

thank u so much.... :)
@ Anirban nasker you are converting the negative number in matrix to positive perfectly fine
but in my case i have i have to first convert negative to postive and again i have to get back to original 'a matrix' can any help in writing the reverse function for it

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Preguntada:

el 11 de Jun. de 2014

Comentada:

el 25 de Dic. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by