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

조회 수: 175 (최근 30일)
suchismita
suchismita 2014년 6월 11일
댓글: Dyuman Joshi 2023년 12월 25일
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
Asif Newaz
Asif Newaz 2019년 11월 12일
u can use the 'sign' function.
Dyuman Joshi
Dyuman Joshi 2023년 12월 25일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Mischa Kim
Mischa Kim 2014년 6월 11일
suchismita, use
a(a<0) = 0;
  댓글 수: 3
juveria fatima
juveria fatima 2018년 9월 24일
@ 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?
KEYUR BORAD
KEYUR BORAD 2021년 2월 25일
Thanks @Mischa Kim this syntax helped me to find if a vector contains both neg and pos value or not!!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Thomas Richner
Thomas Richner 2018년 9월 9일
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 2014년 6월 11일
편집: Anirban Naskar 2014년 6월 11일
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
suchismita
suchismita 2014년 6월 12일
thank u so much.... :)
juveria fatima
juveria fatima 2018년 9월 24일
@ 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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by