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 个评论
Asif Newaz
2019-11-12
u can use the 'sign' function.
An approach to show off (lol) -
a=[0.2 0.1 -0.1 0 0.9]
max(a, 0)
采纳的回答
更多回答(2 个)
Thomas Richner
2018-9-9
6 个投票
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
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
2014-6-12
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
类别
在 帮助中心 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!