How can i remove all the rows of a matrix who contain negative values?

4 views (last 30 days)
Hi, i have a matrix w that i obtain with the code below with random numbers
rng(0)
w_1 = (-0.25 + (1.25-(-0.25)).*rand(1,15))' ; a = ones(15,1); w_2 = a - w_1; w = [w_1, w_2]
w =
0.9721 0.0279
1.1087 -0.1087
-0.0595 1.0595
1.1201 -0.1201
0.6985 0.3015
-0.1037 1.1037
0.1677 0.8323
0.5703 0.4297
1.1863 -0.1863
1.1973 -0.1973
-0.0136 1.0136
1.2059 -0.2059
1.1858 -0.1858
0.4781 0.5219
0.9504 0.0496
So how can i make a matrix that returns all the rows of w who contain only positive elements?
should turn into the matrix above
w=
0.9721 0.0279
0.6985 0.3015
0.1677 0.8323
0.5703 0.4297
0.4781 0.5219
0.9504 0.0496

Accepted Answer

Image Analyst
Image Analyst on 14 Jul 2016
Use > and the all() function:
rng(0)
w_1 = (-0.25 + (1.25-(-0.25)).*rand(1,15))' ;
a = ones(15,1);
w_2 = a - w_1;
w = [w_1, w_2]
allPositiveRows = all(w>0, 2)
wOut = w(allPositiveRows, :)

More Answers (1)

Chris Qin
Chris Qin on 29 Aug 2018
thank you Image Analyst:)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!